-3

I tried to convert a string to a javascript object using JSON.parse but it's not working.

I tried everything but did not get anything on the console.log and there is no error message.

JSON.parse(`{'exp': '1', 'input': '1d6404f66ed3d72e', 'iterate': 'no'}`); 

Update

In the real code, I'm passing the value from an object

console.log(JSON.parse(future.onIOPub.data['text/plain']))
jax
  • 3,927
  • 7
  • 41
  • 70
  • 4
    JSON requires double quotes. – ASDFGerte Sep 14 '21 at 18:10
  • 3
    Because your input isn't valid JSON. I'm pretty sure there _is_ an error message (I'd expect `Unexpected token ' in JSON at position 1`), maybe you are swallowing exceptions somewhere with an empty `catch`? – CherryDT Sep 14 '21 at 18:10
  • I would say it's a dupe of https://stackoverflow.com/questions/4162642/single-vs-double-quotes-in-json/34855065 but people may be nit-picking about the `python` tag. – ASDFGerte Sep 14 '21 at 18:14

1 Answers1

3

When you run this, you should see

Uncaught SyntaxError: Unexpected token ' in JSON at position 1

This error is because you are using single quotes. JSON only accepts double quotes, as described in the spec

https://www.json.org/json-en.html

souenzzo
  • 359
  • 1
  • 6
  • In my real code, I'm not providing quotes manually rather I'm passing value from a variable, I Updated the code. – jax Sep 14 '21 at 18:26
  • can you `console.log(["my-data", future.onIOPub.data['text/plain']])` and print here exactly what it prints? – souenzzo Sep 15 '21 at 15:09
  • It worked I just sliced off the single quote and it. worked. – jax Sep 15 '21 at 15:21