Converting a string to JSON object

JavascriptAjaxJson

Javascript Problem Overview


How do you make JS think that a string is JSON ?

I have a function which only works if JSON object is passed to it. If I pass a string to it, with same format as JSON, it doesn't work. So I want to make that function think that the string passed to it is a JSON. The string is indeed in the JSON format.

I also tried the following. I inputted the string through Ajax , with "handle as" parameter as "JSON", and then when I passed the result to the function it works.

So I deduced the problem is not with the string. How do I convert this string to JSON? If i get same string through ajax request and then passing it to function works, whereas directly passing it doesn't work.

The string is as follows:

  {
     "data": [
   {
  "id": "id1",
      "fields": [
        {
          "id": "name1",
          "label": "joker",
          "unit": "year"
        },
         {"id": "name2", "label": "Quantity"},
	],
      "rows": [    data here....

and closing braces..

Javascript Solutions


Solution 1 - Javascript

var obj = JSON.parse(string);

Where string is your json string.

Solution 2 - Javascript

You can use the JSON.parse() for that.

See docs at MDN

Example:

var myObj = JSON.parse('{"p": 5}');
console.log(myObj);

Solution 3 - Javascript

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

link:-

http://api.jquery.com/jQuery.parseJSON/

Solution 4 - Javascript

I had the same problem with a similar string like yours

{id:1,field1:"someField"},{id:2,field1:"someOtherField"}

The problem here is the structure of the string. The json parser wasn't recognizing that it needs to create 2 objects in this case. So what I did is kind of silly, I just re-structured my string and added the [] with this the parser recognized

var myString = {id:1,field1:"someField"},{id:2,field1:"someOtherField"}
myString = '[' + myString +']'
var json = $.parseJSON(myString)

Hope it helps,

If anyone has a more elegant approach please share.

Solution 5 - Javascript

Simply use eval function.

var myJson = eval(theJsibStr);

Solution 6 - Javascript

convert the string to HashMap using Object Mapper ...

new ObjectMapper().readValue(string, Map.class);

Internally Map will behave as JSON Object

Solution 7 - Javascript

var Data=[{"id": "name2", "label": "Quantity"}]

Pass the string variable into Json parse :

Objdata= Json.parse(Data);

Solution 8 - Javascript

Let's us consider you have string like

example : "name:lucy,age:21,gender:female"

function getJsonData(query){
    let arrayOfKeyValues = query.split(',');
    let modifiedArray =  new Array();
    console.log(arrayOfKeyValues);
    for(let i=0;i< arrayOfKeyValues.length;i++){
        let arrayValues = arrayOfKeyValues[i].split(':');
        let arrayString ='"'+arrayValues[0]+'"'+':'+'"'+arrayValues[1]+'"';
        modifiedArray.push(arrayString);
    }
    let jsonDataString = '{'+modifiedArray.toString()+'}';
    let jsonData = JSON.parse(jsonDataString);
    console.log(jsonData);
    console.log(typeof jsonData);
    return jsonData;
}

let query = "name:lucy,age:21,gender:female";
let response = getJsonData(query);
console.log(response);

`

Solution 9 - Javascript

JSON.parse() function will do.

or

Using Jquery,

var obj = jQuery.parseJSON( '{ "name": "Vinod" }' );
alert( obj.name === "Vinod" );

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZer0View Question on Stackoverflow
Solution 1 - JavascriptKshitijView Answer on Stackoverflow
Solution 2 - JavascriptSarfrazView Answer on Stackoverflow
Solution 3 - Javascriptsandeep patelView Answer on Stackoverflow
Solution 4 - JavascriptAbrahamView Answer on Stackoverflow
Solution 5 - JavascriptSiyavash HamdiView Answer on Stackoverflow
Solution 6 - JavascriptSugan VView Answer on Stackoverflow
Solution 7 - JavascriptAnkita_systematixView Answer on Stackoverflow
Solution 8 - JavascriptShishirView Answer on Stackoverflow
Solution 9 - JavascriptVinod SelvinView Answer on Stackoverflow