0

I have json data which is returning from ajax post

Actual Json Data from Post

{Status: 'True', Message: "['{"text":0.00, "value":"Bonnet", "color": "#682437"}','{"text":1500.00, "value":"Boot lid lock", "color": "#682437"}','{"text":3000.00, "value":"Front bumper", "color": "#682437"}','{"text":1800.00, "value":"Front bumper bracket LH", "color": "#682437"}','{"text":1200.00, "value":"Front bumper grill clips", "color": "#682437"}','{"text":4000.00, "value":"Front bumper upper grill", "color": "#682437"}','{"text":1000.00, "value":"Front wheel housing reinforcement LH", "color": "#682437"}','{"text":3500.00, "value":"Rear tail lamp housing LH", "color": "#682437"}','{"text":2500.00, "value":"Tail lamp LH", "color": "#682437"}','{"text":2500.00, "value":"Tail lamp RH", "color": "#682437"}','{"text":1500.00, "value":"Bonnet", "color": "#682437"}','{"text":1500.00, "value":"Bonnet Alu", "color": "#682437"}','{"text":1500.00, "value":"Bonnet hinge RH", "color": "#682437"}']"}

I did json.parse above response data like below code

var dataObj = JSON.parse(response.d);
console.log(typeof dataObj)  // result as object
console.log(dataObj.Message)  // result as string
['{"text":0.00, "value":"Bonnet", "color": "#682437"}','{"text":1500.00, "value":"Boot lid lock", "color": "#682437"}','{"text":3000.00, "value":"Front bumper", "color": "#682437"}','{"text":1800.00, "value":"Front bumper bracket LH", "color": "#682437"}','{"text":1200.00, "value":"Front bumper grill clips", "color": "#682437"}','{"text":4000.00, "value":"Front bumper upper grill", "color": "#682437"}','{"text":1000.00, "value":"Front wheel housing reinforcement LH", "color": "#682437"}','{"text":3500.00, "value":"Rear tail lamp housing LH", "color": "#682437"}','{"text":2500.00, "value":"Tail lamp LH", "color": "#682437"}','{"text":2500.00, "value":"Tail lamp RH", "color": "#682437"}','{"text":1500.00, "value":"Bonnet", "color": "#682437"}','{"text":1500.00, "value":"Bonnet Alu", "color": "#682437"}','{"text":1500.00, "value":"Bonnet hinge RH", "color": "#682437"}']

now i want to split this dataObj.message to make it as array

like text field as arText value field as arValue color field as arColor

After that i will populate these array to chart. so that the chart will be generating it.

I was trying to again json.parse data

var data = JSON.parse(dataObj.Message);
//var data = JSON.parse(JSON.stringify(data));

I got below error message

M742:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1
    at JSON.parse (<anonymous>)
    at Object.success (Home.aspx:742:20)
    at u (jquery.min.js:2:27457)
    at Object.fireWith [as resolveWith] (jquery.min.js:2:28202)
    at k (jquery.min.js:2:77651)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2:79907)
    at Object.send (jquery.min.js:2:80266)
    at Function.ajax (jquery.min.js:2:77118)
    at Function.s.ajax.s.ajax (jquery-migrate-3.3.2.min.js:2:3931)
    at fetchData (Home.aspx:724:5)

can you please help on this? I tried some examples from stackoverflow. it did not work.

6
  • ['{"text":0.00, "value":"Bonnet", "color": "#682437"}', ... ,'{"text":1500.00, "value":"Bonnet hinge RH", "color": "#682437"}'] is not valid JSON. Try JSON.parse(dataObj.message[0])? Commented Jan 20, 2022 at 20:16
  • I verified json data online and good. I tried it but it's not working**` Unexpected end of Json Input at Json.parse **. Got this error. Updated actual json data. Commented Jan 20, 2022 at 20:24
  • 1
    The problem is all the single quotes around each object in your dataObj.message. If you get rid of those then the json parses fine. Doesn't parse: ['{"text":0.00, "value":"Bonnet", "color": "#682437"}','{"text":1500.00, "value":"Boot lid lock", "color": "#682437"}','{".... Does parse: [{"text":0.00, "value":"Bonnet", "color": "#682437"},{"text":1500.00, "value":"Boot lid lock", "color": "#682437"},{"... Commented Jan 21, 2022 at 1:22
  • Thanks @ourmandave. I have removed single quote (') from each objects. It's working... Commented Jan 23, 2022 at 8:18
  • Is the ‘JSON’ generated by a Python script by any chance? Commented Jun 26, 2022 at 20:03

1 Answer 1

1

as others have pointed out in the comments, your string is NOT valid json, but it's pretty close to valid json.. with some minor slice/split hacking, we can parse it:

str=`{Status: 'True', Message: "['{"text":0.00, "value":"Bonnet", "color": "#682437"}','{"text":1500.00, "value":"Boot lid lock", "color": "#682437"}','{"text":3000.00, "value":"Front bumper", "color": "#682437"}','{"text":1800.00, "value":"Front bumper bracket LH", "color": "#682437"}','{"text":1200.00, "value":"Front bumper grill clips", "color": "#682437"}','{"text":4000.00, "value":"Front bumper upper grill", "color": "#682437"}','{"text":1000.00, "value":"Front wheel housing reinforcement LH", "color": "#682437"}','{"text":3500.00, "value":"Rear tail lamp housing LH", "color": "#682437"}','{"text":2500.00, "value":"Tail lamp LH", "color": "#682437"}','{"text":2500.00, "value":"Tail lamp RH", "color": "#682437"}','{"text":1500.00, "value":"Bonnet", "color": "#682437"}','{"text":1500.00, "value":"Bonnet Alu", "color": "#682437"}','{"text":1500.00, "value":"Bonnet hinge RH", "color": "#682437"}']"}`;

arr = JSON.parse(str.split("Message: \"",2)[1].slice(0,-2).split("'").join(""))

works! :) ... it's very hack though, and ideally you should instead get the guy who wrote the json-generator to fix his json generator, it's broken, and it generates broken json. enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.