cancel
Showing results for 
Search instead for 
Did you mean: 

Convert Text String to JSON object

Former Member
0 Kudos

Hi,

How can a string be converted to a JSON object in SAPUI5 ?

Example : var mystr = ' [ { name : "Joe", last : "Doe"}, { name : "Jack", last : "Daniel" }]';

How do i convert mystr to a JSON object which can be used in a JSONModel?

$,parseJSON(mystr) or jQuery.parseJSON(myStr) do not seem to exist.

JSONModel.setJSON(mystr) too did not work.

Any pointers ?

regards,

sreeram

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

$.parseJSON should exist.

Try to change your json string to this since your string is not a valid json (you can check this here: ) JSONLint - The JSON Validator.

var mystr = '[{ "name" : "Joe", "last" : "Doe"}, { "name" : "Jack", "last" : "Daniel" }]';

Take a look here:

JSFiddle

Greets,

ben

Former Member
0 Kudos

Hi Ben,

Thanks for the reply. I'm not really concerned about the format of the string.

I'm concerned with the string to JSON convertor API. parseJSON or setJSON.

As i mentioned earlier, $.parseJSON is not there. I may be missing some library ???

regards,

sreeram

seVladimirs
Active Contributor
0 Kudos

are you sure that you have this kind of string? I assume definition of name/last should be surrounded by quotes in order to have correct JSON model, like that:

var mystr = '[ { "name" : "Joe", "last" : "Doe"}, { "name" : "Jack", "last" : "Daniel" } ]';

here is working example:

JS Bin - Collaborative JavaScript Debugging</title> <link rel="icon" href="h...

Former Member
0 Kudos

Hi Sreeram,

when you want to use parseJSON you need a valid json string, and a valid json string requires the quotes for the name attributes. (well explained here: javascript - JSON syntax for property names - Stack Overflow)

I've just tested  the code posted above locally with SAPUI5 Version 1.16 and it works well - the parseJSON exists as expected since it's part of JQuery since jquery version 1.4.1)  so I'm not sure what's wrong with your installation...

greets,

ben

Answers (3)

Answers (3)

Former Member
0 Kudos

Simply use


var jsonObject = eval(mystr);

Former Member
0 Kudos

Please do not!

Reasons why you should avoid eval are explained here:

How evil is eval? | JavaScript, JavaScript...

Former Member
0 Kudos

You're right. However, the jQuery.parseJSON function will not work for the example above, as it is not a valid JSON. It's an array object of JSON sub objects.

Former Member
0 Kudos

Hi Ben, Chandra, Vladimir,

My mistake. As both Ben and Vladimir have pointed out, the parseJSON expects string with double quotes around the property names.

I came to the wrong conclusion, since the test object could be constructed in Javascript without the quotes. Also, the documentation and editor's choices did not have "parseJSON'.

Now, my code is working.

regards,

sreeram

ChandraMahajan
Active Contributor
0 Kudos

Hi,

you can do as below,

var oModel = new sap.ui.model.json.JSONModel();

oModel.setData( [ { name : "Joe", last : "Doe"}, { name : "Jack", last : "Daniel" }] );


Regards,

Chandra

Former Member
0 Kudos

Hi Chandra,

thanks for your reply.

I specifically want to build a JSON like string and then convert it to a JSON object.

Apparently SAPUI5 does not seem to have any API to do so.

regards,

sreeram