0

I have a returned string from my server and I want to parse it into a JSON object, the following is the string and what I am doing :

stringToParse = "\"{'female': 16, 'brand': 75, 'male': 8}\""
dataJson = JSON.parse(stringToParse)
console.log(dataJson)
console.log(dataJson.male)

I'm getting this as output :

{'female': 16, 'brand': 75, 'male': 8}
undefined

so I can't access the male, female, and brand objects within the JSON.

2
  • 3
    The dataJson is a string Commented Jul 10, 2020 at 11:48
  • how could it be,isn't the JSON.parse returns a JSON object ? Commented Jul 10, 2020 at 11:49

5 Answers 5

4

correct json to be parsed should be

stringToParse = '{"female": 16, "brand": 75, "male": 8}'

you need to alter the code at your server to return data in this manner, or handle it in your js file.

This code works

let stringToParse = '{"female": 16, "brand": 75, "male": 8}'
dataJson = JSON.parse(stringToParse)
console.log(dataJson)
console.log(dataJson.male)

If, though, you can't alter the original stringToParse, then try this in order to parse it at js

let stringToParse = "\"{'female': 16, 'brand': 75, 'male': 8}\""
JSON.parse(stringToParse.replace(/\"/g, '').replace(/'/g, '"'))
Sign up to request clarification or add additional context in comments.

9 Comments

really hard to change the server's side, you know how I can easily switch to that in JS?
the easiest solution would be to double parse it JSON.parse(JSON.parse(stringToParse))
Updated my answer with that
@FahdLyousfi check my updated answer. I think JSON.parse(stringToParse.replace(/\"/g, '').replace(/'/g, '"')) will do the trick
Unexpected token ' in JSON at position 1 at JSON.parse (<anonymous>)
|
2

There is something wrong with you string I think those extra quotation marks are useless. The JSON parser things that you providing him with a string "{"female": 16, "brand": 75, "male": 8}" and it parses it as string so you see the console.log result {"female": 16, "brand": 75, "male": 8} but it is not an objet the whole thing is a string. Remove extra quotation marks and it will think it is an object.

stringToParse = '{"female": 16, "brand": 75, "male": 8}'
dataJson = JSON.parse(stringToParse)
console.log(dataJson)
console.log(dataJson.male)

Comments

1

Your json string is invalid format. In JSON, keys must be strings written with double quotes not in single quotes. e.g. {"male":16}. Try to read this https://www.w3schools.com/js/js_json_syntax.asp. So below is the correct answer :

stringToParse = '{"female": 16, "brand": 75, "male": 8}'
dataJson = JSON.parse(stringToParse)
console.log(dataJson)
console.log(dataJson.male)

Comments

0

In your second line parse like this

stringToParse = "\"{'female': 16, 'brand': 75, 'male': 8}\""
dataJson = JSON.parse(stringToParse.replace(/\"/g, '').replace(/'/g, '"'))

stringToParse = "\"{'female': 16, 'brand': 75, 'male': 8}\""
dataJson = JSON.parse(stringToParse.replace(/\"/g, '').replace(/'/g, '"'))
console.log(dataJson)
console.log(dataJson.male)

Comments

0

I could solve this problem by using:

dataJson  = JSON.parse(decodeURI(stringToParse));

here is a reference to it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI

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.