0

I have below result returned from python script

{"a_paget_wilkes": "\/speaker\/a_paget_wilkes.json",
"aaron_clark": "\/speaker\/aaron_clark.json",
"aaron_dunlop": "\/speaker\/aaron_dunlop.json",
"aaron_ernst": "\/speaker\/aaron_ernst.json",
"aaron_hurst": "\/speaker\/aaron_hurst.json",
"abigail_miller": "\/speaker\/abigail_miller.json",
"abner_kauffman": "\/speaker\/abner_kauffman.json"}

So it is pretty well formatted JSON I believe. Javascript variable which has above data is called jsondata. Now in the chrome developer tool console when I try to access key valye pair by typing jsondata. I expect all keys to be listed as suggestion, but it shows me string properties like length, anchor, big, blink etc... instead

I tried even JSON.stringify first and then JSON.parse but still the same!!!

Any idea what is wrong in here?

1 Answer 1

1

jsondata is apparently a string containing your JSON, rather than a JavaScript object that would result from parsing your JSON.

To parse it, use JSON.parse.

I tried even JSON.stringify first and then JSON.parse but still the same!!!

JSON.stringify will take you in the wrong direction — it will wrap your entire string in a JSON string — and JSON.parse will only undo the JSON.stringify (recovering your original string), not parse your original string.

You need to call JSON.parse without calling JSON.stringify first.

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

2 Comments

Now I am getting below Uncaught SyntaxError: Unexpected token ' in JSON at position 1 at JSON.parse (<anonymous>) at index.js:16 code where I parse it is pyshell.PythonShell.run('py/speakers.py', null, function (err, res) { if (err){ alert('Error :' + err); } else { var jsonDat = JSON.parse(res[2].toString()); console.log(jsonDat); } });
No wait....I just got what I was doing wrong! I was returning json.loads from my python script instead of json.dumps. I just corrected that and now all is well!!! Thanks for help, much appreciated.

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.