cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert the odata field value from html format to JSON format?

former_member195820
Participant
0 Kudos

Hi all,

         I use  the following  successfactor's odata for one of  my app:

     "   https://apisalesdemo8.successfactors.com/odata/v2/JobRequisitionLocale  " 


The  'jobDescription' field of this odata is in HTML format.


<d:jobDescription>

<p> The Account Manager is a sales professional with a history of over-performance in quota attainment, winning new business and developing existing accounts

.<br /> <br /> <b>Responsibilities</b><br /> The successful candidates will possess the following qualifications:</p> <ul> <li> 8-10 year background of

successful sales experience to Fortune 2000 accounts</li> <li> Must have a history of exceeding annual sales quotas for last 3-5 consecutive years.

</li> <li> Ability to manage major account sales cycles to global Fortune 2000 organizations</li> <li> Will have key contacts in the software industry.

</li> <li> Excellent written/verbal communication skills to interact with senior level corporate management.</li> <li> Operates with a sense of urgency

and has a positive, winning attitude.</li> <li> Proven ability to prospect and manage a designated territory to maximize revenue growth.</li> </ul>

</d:jobDescription>


Now, I need to convert that HTML format into json object to use it in my app.I tried the following code.

var jdescs=oData.results[0].jobDescription;

var Odata=jdescs.JSON();

alert(JSON.stringify(Odata));

But it's showing me " Uncaught TypeError: jdescs.JSON is not a function".

Can someone help me with this.

Thanks,

Ramya

Accepted Solutions (1)

Accepted Solutions (1)

former_member195820
Participant
0 Kudos

Now,I'm able to strip out the HTML & display only  the text when I tried the following code:

  var jdescs=oData.results[0].jobDescription; 

  strip(jdescs);                   //function call to strip function

  function strip(html)

  {

    var tmp = document.createElement("DIV");

    tmp.innerHTML = html;

    m=tmp.textContent || tmp.innerText || "";

    return m;

  }

sap.ui.getCore().byId("ljobdesc").setText(m);   //displays only the text by removing html

Thanks all for valuable replies.

Regards,

Ramya

Answers (3)

Answers (3)

saivellanki
Active Contributor
0 Kudos

Hi Ramya,

You mean to say, you're getting data something like this:


<div id="jobDescription">someValue</div>

If so, you can try something like this:


var oHTML = <div id="jobDescription">someValue</div>          //HTML Element

var oValue = oHTML.textContent;          //Get the value which is inside HTML Element

var nObject = {"jobDescription": oValue};          //Create a JSON Object

If not above, set a console.log(oHTML) and share us the format.

Regards,

Sai Vellanki.

former_member195820
Participant
0 Kudos

Hi Sai Vellanki,

When I pass the odata in Restclient, I get the value for jobDescription field similar to the below image:

I need to display only the text (excluding all the HTML tags between them ) like the following image.

Code:

   var jjtitle=oData.results[0].jobTitle;

             // alert("jtilte:"+jjtitle);

            sap.ui.getCore().byId("jobtitle").setText("Job tilte: "+jjtitle);

            var jdescs=oData.results[0].jobDescription;

          //  alert(jdescs)

          

            var oHTML = jdescs;          //HTML Element

            var oValue = oHTML.textContent;          //Get the value which is inside HTML Element

            var nObject = {"jobDescription": oValue};

            console.log(oHTML);

Here's what I'm getting in console after I tried what you told me...

         

Thanks,

Ramya

ChrisSolomon
Active Contributor
0 Kudos

So you want to strip out the HTML and keep only the text? There are PLENTY of solutions for this all over the web if you just Google that.

ChrisSolomon
Active Contributor
0 Kudos

So wait....

You read the "job description" field from OData.

The "job description" field is formatted HTML text.

You now want to convert the formatted HTML text into JSON.

Why?!?!?!? If that is the case.....simply Google "convert HTML to JSON" and you will find LOTS of hits!

former_member203031
Contributor
0 Kudos

Hi Ramya,

I think now u r getting the value of jobDescription in to the variable jdescs.

now you have to store in json format

to store in JSON format :

var json = { }                //create one empty object

json.value = jdescs ;      // now the job description will be stored value in json format.

after that u can check in console the json.

you will get the following json{"value":"jobdescription"}.

I hope it will works for you.

Thanks,

Deepak Raj.

former_member195820
Participant
0 Kudos

Hi Deepak,

Thanks for the reply.

I tried your code .But i didn't get Json format.

Regards,

Ramya

former_member203031
Contributor
0 Kudos

Hi Ramya,

Can you show your code and as well as any errors if you are getting from the console,

Thanks,

Deepak Raj.