pankaj
pankaj

Reputation: 159

How to convert to JSONObject

enter image description hereBelow code:

grid.getStore().getAt(0).data.ServiceDetails

Output:

"{brief: {"totalBilledUser":3}, details:{"totalBilledUser":3, "totalBilledUser1":3, "totalBilledUser2":3, "totalBilledUser3":3, "totalBilledUser4":3, "totalBilledUser5":3}}"

Please note that here the output starts with double quote.

  1. JSON.parse(grid.getStore().getAt(0).data.ServiceDetails);
    
    output Uncaught SyntaxError: Unexpected token b in JSON at position 1
  2. JSON.parse(JSON.stringify(grid.getStore().getAt(0).data.ServiceDetails))
    

    It returns original string back.

    Please guide me the proper way to get it as JSON object.

Upvotes: 0

Views: 92

Answers (1)

David Daza
David Daza

Reputation: 11

the quotes are the problem, take a look:

"{ brief: { "totalBilledUser": 3 } }"

The first " marks the beginning of the string, but the second " instead of open the strinf for totalBilledUser, closes the first ".

Possible solutions are:

  1. Open and close the output with single quotation marks '.

    '{ brief: { "totalBilledUser": 3 } }'
    
  2. Escape the double quotes inside the output:

    "{ brief: { \"totalBilledUser\": 3 } }"
    

Once you achieve one of the above solutions, you'll have, at least, a valid string. Now you can use JSON.stringify and then JSON.parse to transform it into JSON format. Hope I make myself clear.

Upvotes: 1

Related Questions