Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why JSON.parse twice? #78

Open
constantingoeldel opened this issue Jan 19, 2023 · 2 comments
Open

Why JSON.parse twice? #78

constantingoeldel opened this issue Jan 19, 2023 · 2 comments
Assignees

Comments

@constantingoeldel
Copy link
Collaborator

const studyJSON = JSON.parse(JSON.parse(JSON.stringify(values[0])));

@marcotxma
Copy link
Contributor

It's weird that its even working, given that this code is trying to JSON.parse an already parsed object. I'm pretty sure the whole thing can be replaced by values[0]. I'll test it

@marcotxma
Copy link
Contributor

marcotxma commented Jan 31, 2023

I looked into it a bit deeper and here is what I've found, the study, a.k.a. values[0], is saved as a JSON string. Now apparently, you can stringify a JSON string (I've tested it by running JSON.stringify(JSON.stringify({a: 1, b: 2}))). This will give a strange result though. All in all, whats happening here is:

  1. the values[0] JSON string gets stringified JSON.stringify(values[0])
  2. the JSON stringified string, gets unstringified, resulting in a normal JSON string JSON.parse(JSON.stringify(values[0]))
  3. the JSON string gets unstringified and is therefore finally a javascript object JSON.parse(JSON.parse(JSON.stringify(values[0])))

which leads to the conclusion, that this should be replaceable by JSON.parse(values[0])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants