How to parse multiple object in json in javascript?

Related searches

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={}
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?

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

How to parse multiple objects using JSON.parse(), JSON stands for JavaScript Object Notation. JSON is extremely lightweight Multiple key/value pairs are separated by a comma , . Array: This is defined as an � Parsing JSON Data in JavaScript In JavaScript, you can easily parse JSON data received from the web server using the JSON.parse () method. This method parses a JSON string and constructs the JavaScript value or object described by the string. If the given string is not valid JSON, you will get a syntax error.

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

var jsonObj = JSON.parse(object.body)


// 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"}

JavaScript JSON Parsing, I am trying to parse the results of an AD Query. This query can return multiple objects in a JSON string. I believe they're all being put into my� The JSON.parse () method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned. JavaScript Demo: JSON.parse ()

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


var dict={}

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'])

Parse Multiple Objects from JSON String, JavaScript: Parse multiple JSON documents from string. gistfile1.js. /**. * Parses a string containing one or multiple JSON encoded objects in� x is the parameter .which i have used to define function and while calling function , i have passed the product catalog as the parameter and found keys and subkeys..if you have another JSON with similar structure , you can use the same function and pass that JSON object as the parameter. – Naga Sai A Jun 15 '16 at 21:06

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);

Hope it helps.

JavaScript: Parse multiple JSON documents from string � GitHub, parse multiple json objects javascript parse multiple json objects python json multiple objects how to send multiple json objects in postman parse nested json� You have got an array Of Objects inside Array. Firstly, you need to parse the data from JSON to object using JSON.parse (data); Then, access the object offers. using the parsedData.offers object and then iterate over the array to get the status.

Sending/Parsing multiple JSON objects, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� While you can convert text to an object with the eval() function, it is not very secure, so we’ll use the JSON.parse() function instead. To convert the example in the JSON.stringify() section above, we would pass the string s to the function, and assign it to a new variable: var o = JSON.parse(s) Then, we would have the object o to work with, which would be identical to the object obj.

JSON Arrays, Converting a JSON Text to a JavaScript Object. A common use of JSON is to read data from a web server, and display the data in a web page. For simplicity, this can be demonstrated using a string as input. First, create a JavaScript string containing JSON syntax:

Assuming that you have the above in a String called input (and that the JSON is valid): var obj = JSON.parse(input) // converts it to a JS native object. // you can descend into the new object this way: var As a warning, earlier versions of IE do not have JSON.parse, so you will need to use a framework for that.

  • are you sure that body is a valid JSON string?
  • I can see that body has string rather than an object. this json object is not valid - check in
  • parse the JSON string you have stored at the body property: console.log("Status color is", JSON.parse(dict.body)['statusColor'])
  • 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.