0

I have following json to be parsed and need to make different object


var object={"statusCode": 200, "headers": {}, "body": "{\"statusColor\": {\"abc1\": \"green\", \"abc2\": \"red\", \"bcd11\": \"red\", \"bcd2\": \"green\", \"cde1\": \"green\", \"cde2\": \"red\", \"efg1\": \"red\", \"efg2\": \"red\", \"efg3\": \"red\"}, \"time\": 
{\"abc\": \"2020-02-03 11:41:40\", \"bcd\": \"2020-02-03 11:41:40\", \"cde\": \"2020-02-03 11:41:40\", \"efg\": \"2020-02-03 11:41:40\"}}"}

//Iam trying to get the seperate dict object for statusColor and time as below

var dict={}
dict=object
console.log("Status color is", dict.body['statusColor'])

#Out put is 'undefined'

Expected is : statusColor= 'get dict of all status color' similarly for time so what is the best approach to get the object dict from the multi json object?

3
  • 1
    are you sure that body is a valid JSON string? Commented Feb 3, 2020 at 6:49
  • I can see that body has string rather than an object. this json object is not valid - check in jsonlint.com Commented Feb 3, 2020 at 6:50
  • parse the JSON string you have stored at the body property: console.log("Status color is", JSON.parse(dict.body)['statusColor']) Commented Feb 3, 2020 at 6:52

4 Answers 4

1

You can use JSON.parse() method to parse valid JSON string.

var jsonObj = JSON.parse(object.body)

 console.log(jsonObj.statusColor)
 console.log(jsonObj.time)

// output 
// statusColor: {abc1: "green", abc2: "red", bcd11: "red", bcd2: "green", cde1: "green", …}
// time: {abc: "2020-02-03 11:41:40", bcd: "2020-02-03 11:41:40", cde: "2020-02-03 11:41:40", efg: "2020-02-03 11:41:40"}
Sign up to request clarification or add additional context in comments.

2 Comments

If you click the [<>] you can create a snippet like the other answers - not that it is needed anymore
Thank you @Rezwanul. This got worked. Actually i didn't check the return type of body which i assumed that it could be a dictionary.
1

You have an invalid newline and you need to parse the body

Also

var dict={}
dict=object

is not copying the object, just creating another pointer to it

Working code:

const object = {
  "statusCode": 200,
  "headers": {},
  "body": "{\"statusColor\": {\"abc1\": \"green\", \"abc2\": \"red\", \"bcd11\": \"red\", \"bcd2\": \"green\", \"cde1\": \"green\", \"cde2\": \"red\", \"efg1\": \"red\", \"efg2\": \"red\", \"efg3\": \"red\"}, \"time\":  {\"abc\": \"2020-02-03 11:41:40\", \"bcd\": \"2020-02-03 11:41:40\", \"cde\": \"2020-02-03 11:41:40\", \"efg\": \"2020-02-03 11:41:40\"}}"
}
const dict = JSON.parse(object.body)

console.log("Status color is", dict['statusColor'])

Comments

0

You need to parse your JSON data in order to remove the escape slashes from the body object. The undefined error is because there is no key statusColor in the body object. If you log only the body object(if only this object is valid in JS) then you'll see the string

use const parsedBody = JSON.parse(object.body) and then try to refer to parsedBody.statusColor

Comments

0

Try parsing the json first.

var object={"statusCode": 200, "headers": {}, "body": "{\"statusColor\": {\"abc1\": \"green\", \"abc2\": \"red\", \"bcd11\": \"red\", \"bcd2\": \"green\", \"cde1\": \"green\", \"cde2\": \"red\", \"efg1\": \"red\", \"efg2\": \"red\", \"efg3\": \"red\"}, \"time\": {\"abc\": \"2020-02-03 11:41:40\", \"bcd\": \"2020-02-03 11:41:40\", \"cde\": \"2020-02-03 11:41:40\", \"efg\": \"2020-02-03 11:41:40\"}}"};
 var dict = JSON.parse(object.body);
console.log(dict.statusColor);

Hope it helps.

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.