how to parse a json data with multiple values? - javascript
i have a multidimensional json . i want to parse it to get the values.
var json='{"Links":[],"RequestedObject":{"FieldContents":{"21514":{"Type":1,"IsError":false,"Value":"Saneen","FieldId":21514,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21516":{"Type":1,"IsError":false,"Value":"English","FieldId":21516,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21517":{"Type":1,"IsError":false,"Value":"Malayalam","FieldId":21517,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21515":{"Type":2,"IsError":false,"Value":26.0,"FieldId":21515,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21518":{"Type":2,"IsError":false,"Value":80.0,"FieldId":21518,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21519":{"Type":2,"IsError":false,"Value":40.0,"FieldId":21519,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21520":{"Type":4,"IsError":false,"Value":{"ValuesListIds":[72639],"OtherText":null},"FieldId":21520,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21523":{"Type":3,"IsError":false,"Value":"2017-03-29T00:00:00","FieldId":21523,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21510":{"Type":6,"FieldId":21510,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21511":{"Type":21,"FieldId":21511,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21512":{"Type":22,"FieldId":21512,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21521":{"Type":11,"Value":null,"FieldId":21521,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}}}},"IsSuccessful":true,"ValidationMessages":[]}';
i have tried with JSON.parse , but no luck.
Here is an simple example of JSON.parse() and here is document for that:
var json='{"Links":[],"RequestedObject":{"FieldContents":{"21514":{"Type":1,"IsError":false,"Value":"Saneen","FieldId":21514,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21516":{"Type":1,"IsError":false,"Value":"English","FieldId":21516,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21517":{"Type":1,"IsError":false,"Value":"Malayalam","FieldId":21517,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21515":{"Type":2,"IsError":false,"Value":26.0,"FieldId":21515,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21518":{"Type":2,"IsError":false,"Value":80.0,"FieldId":21518,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21519":{"Type":2,"IsError":false,"Value":40.0,"FieldId":21519,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21520":{"Type":4,"IsError":false,"Value":{"ValuesListIds":[72639],"OtherText":null},"FieldId":21520,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21523":{"Type":3,"IsError":false,"Value":"2017-03-29T00:00:00","FieldId":21523,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21510":{"Type":6,"FieldId":21510,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21511":{"Type":21,"FieldId":21511,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21512":{"Type":22,"FieldId":21512,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21521":{"Type":11,"Value":null,"FieldId":21521,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}}}},"IsSuccessful":true,"ValidationMessages":[]}';
var obj = JSON.parse(json);
console.log(obj);
// retrieve particular fields
var newFieldContents = {};
for(var key in obj['RequestedObject']['FieldContents']){
var o = obj['RequestedObject']['FieldContents'][key];
newFieldContents[key] = {
Value: o['Value'],
FieldId: o['FieldId']
}
}
console.log(newFieldContents);
Update
Add how to retrieve particular fields.
Related
How to retrieve JSON keys without any buffer error?
Below is my JSON that I get from reading a text file using node js readFileSync. {"MESSAGE":"Triggered for ID 453289","STATUS":"02","APPROVAL_COMPLETED":""} Now when I try to get the keys of the JSON using the code below Object.keys(json); I ge the below error The keys are 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114 Also if I try to get the value of the JSON element like below json["STATUS"]; // I also used json.status (neither works) I get the value as undefined. Below is the entire code var fs = $.require('fs'); var stream = fs.readFileSync('api.txt', 'utf8'); console.log("Global " +JSON.stringify(stream)); var data = JSON.parse(stream); var retrievedData = JSON.stringify(data.retrievedData); var json = JSON.parse(retrievedData); console.log("The API Output is " +json); var keys = Object.keys(json); console.log("The keys are "+keys); var flag = json["STATUS"]; //var retrievedData = stream.retrievedData; console.log("flag is "+flag); Below are the particulars Output of Data is {"retrievedData":"{\"MESSAGE\":\"Triggered for Group ID 453289\",\"STATUS\":\"02\",\"APPROVAL_COMPLETED\":\"\"}","statusCode":200,"MESSAGE":"API successfully called"} Output of retrievedData is {"MESSAGE":"Triggered for ID 453289","STATUS":"02","APPROVAL_COMPLETED":""} Please help in resolving the issues.
The problem is that your data is a string, so you need to parse it into an object: let input = '{"MESSAGE":"Triggered for ID 453289","STATUS":"02","APPROVAL_COMPLETED":""}' console.log(Object.keys(input)); let parsed = JSON.parse(input); console.log(Object.keys(parsed)); console.log(parsed['STATUS']); The error in your code is calling var retrievedData = JSON.stringify(data.retrievedData); - the data appears to already a string at that point, so you double encode it. You can just skip the JSON.stringify and it would work: var stream = `{ "retrievedData": "{\\"MESSAGE\\":\\"Triggered for ID 453289\\",\\"STATUS\\":\\"02\\",\\"APPROVAL_COMPLETED\\":\\"\\"}" }` var data = JSON.parse(stream); var retrievedData = data.retrievedData; var json = JSON.parse(retrievedData); console.log("The API Output is " +json); var keys = Object.keys(json); console.log("The keys are "+keys); var flag = json["STATUS"];
Dont stringify the JSON when passing it as a parameter to Object.keys(). var keys = Object.keys(json); Instead of var keys = Object.keys(JSON.stringify(json));
You can directly apply a loop on retrievedData to find keys:- for(var temp in retrievedData) { console.log(temp) } Here temp will be your keys
Try and change var keys = Object.keys(JSON.stringify(json)); towards var keys = Object.keys(json); Does that help to resolve your issue?
how to convert array with index value into json in angular js
what is the best way to convert this array in to json in angular js: here i need to convert this data into json 0:"1800-300-1947" 1:"112" 2:"8529631485" 3:"7299490148" i have tried var myJsonString =JSON.stringify(data); but its not working it shows like '[]' in the log In this code i am getting the value from array and pushed into the another array called data. if i log the data i got the above result. how to convert that into json here i have added my code for your reference var data = Array(); for (var i = 0; i < contacts.length; i++) { var contact = contacts[i].phoneNumbers[j].value; console.log(contact); data.push(contact); } var myJsonString =JSON.stringify(data); console.log(myJsonString);
Please refer below code for reference its working. var contacts = []; contacts[0] = {"phoneNumbers": ["1800-300-1947"]}; contacts[1] = {"phoneNumbers": ["112"]}; contacts[2] = {"phoneNumbers": ["8529631485"]}; contacts[3] = {"phoneNumbers": ["7299490148"]}; var myJsonString = JSON.stringify(contacts); console.log(myJsonString);
Get array from json using javascript
Server returns me such object, but i need only array ITEMS. How can i get it? I tried array['items'] but the result is undefiend { "items": [ { .. }, { .. }, { .. } ],.. ,.. }
// JSON string returned from the server var text = '{"items":[{"name":"itemX" ,"price":15},{"name":"itemY","price":25},{"name":"itemZ","price":20}]}'; // Convert the string returned from the server into a JavaScript object. var object = JSON.parse(text); // Accessing a specific property value of an item console.log(object.items[0].name); //itemX // Extract 'items' in to a separate array var itemsArray = object.items; console.log(itemsArray); //[itemObject, itemObject, itemObject]
If you're getting this as a string: var json = JSON.parse(my_json_string) Then, var keys = Object.keys(json); var values = []; for(var i = 0; i < keys.length; i++){ values.push(json[keys[i]]); }
JSON rows Extracting issue in JavaScript
I have following json data coming from server in which i want to extract LimitClass and LimitClassID and store their values in respective arrays. { "ErrorDesc":"", "ErrorCode":"", "LimitClassList":"[{\"LimitClass\":\"L16\\n\",\"LimitClassId\":\"32900\\n\"},{\"LimitClass\":\"28febL0\\n\",\"LimitClassId\":\"31901\\n\"},{\"LimitClass\":\"L14\\n\",\"LimitClassId\":\"31900\\n\"},{\"LimitClass\":\"L17\\n\",\"LimitClassId\":\"32950\\n\"},{\"LimitClass\":\"L15\\n\",\"LimitClassId\":\"31950\\n\"},{\"LimitClass\":\"L0\\n\",\"LimitClassId\":\"21901\\n\"},{\"LimitClass\":\"L4\\n\",\"LimitClassId\":\"23000\\n\"},{\"LimitClass\":\"OTC Send\\n\",\"LimitClassId\":\"30901\\n\"},{\"LimitClass\":\"L2\\n\",\"LimitClassId\":\"22900\\n\"},{\"LimitClass\":\"L12\\n\",\"LimitClassId\":\"28900\\n\"},{\"LimitClass\":\"L6\\n\",\"LimitClassId\":\"23900\\n\"},{\"LimitClass\":\"L1\\n\",\"LimitClassId\":\"25900\\n\"},{\"LimitClass\":\"L13\\n\",\"LimitClassId\":\"29900\\n\"},{\"LimitClass\":\"L7\\n\",\"LimitClassId\":\"24900\\n\"},{\"LimitClass\":\"L8\\n\",\"LimitClassId\":\"26900\\n\"},{\"LimitClass\":\"L10\\n\",\"LimitClassId\":\"27900\\n\"},{\"LimitClass\":\"L13\\n\",\"LimitClassId\":\"30900\\n\"},{\"LimitClass\":\"UatTesting123\\n\",\"LimitClassId\":\"32901\\n\"}]" } Here is the code I have tried : var list = data.LimitClassList; var arrayLimitClass = []; var arrayLimitClassId = []; for(var i in list) { arrayLimitClass.push(list[i].LimitClass); arrayLimitClassId.push( list[i].LimitClassId); } alert(list); alert(arrayLimitClass); alert(arrayLimitClassId); List variable has following result when I alert it: [{\"LimitClass\":\"L16\\n\",\"LimitClassId\":\"32900\\n\"},{\"LimitClass\":\"28febL0\\n\",\"LimitClassId\":\"31901\\n\"},{\"LimitClass\":\"L14\\n\",\"LimitClassId\":\"31900\\n\"},{\"LimitClass\":\"L17\\n\",\"LimitClassId\":\"32950\\n\"},{\"LimitClass\":\"L15\\n\",\"LimitClassId\":\"31950\\n\"},{\"LimitClass\":\"L0\\n\",\"LimitClassId\":\"21901\\n\"},{\"LimitClass\":\"L4\\n\",\"LimitClassId\":\"23000\\n\"},{\"LimitClass\":\"OTC Send\\n\",\"LimitClassId\":\"30901\\n\"},{\"LimitClass\":\"L2\\n\",\"LimitClassId\":\"22900\\n\"},{\"LimitClass\":\"L12\\n\",\"LimitClassId\":\"28900\\n\"},{\"LimitClass\":\"L6\\n\",\"LimitClassId\":\"23900\\n\"},{\"LimitClass\":\"L1\\n\",\"LimitClassId\":\"25900\\n\"},{\"LimitClass\":\"L13\\n\",\"LimitClassId\":\"29900\\n\"},{\"LimitClass\":\"L7\\n\",\"LimitClassId\":\"24900\\n\"},{\"LimitClass\":\"L8\\n\",\"LimitClassId\":\"26900\\n\"},{\"LimitClass\":\"L10\\n\",\"LimitClassId\":\"27900\\n\"},{\"LimitClass\":\"L13\\n\",\"LimitClassId\":\"30900\\n\"},{\"LimitClass\":\"UatTesting123\\n\",\"LimitClassId\":\"32901\\n\"}] But I am getting dots (.) when I alert arrayLimitClass and arrayLimitClassId. What am I doing wrong in extracting rows of json Object?
"LimitClassList":"[{\"LimitClass\":\"L1....]" ^ ^ LimitClassList is a string, not an array. Make it so it is an actual array, than your code should work. There should be no reason to have to parse it again.
The value below data.LimitClassList is itself a String containing JSON. You have to decode this first. var list = JSON.parse( data.LimitClassList ); var arrayLimitClass = []; var arrayLimitClassId = []; // ... This is more or less a workaround. You should have a look at your server code and fix the encoding error there!
Get the Inner Array Value of JSON in Javascript
I need the basic Help , I have a json object in the following format Data = {"line":"1", "column":"1" "response":[{"criteria":"starts","response":"358","field":"S"}, {"criteria":"ends","response":"359","field":"H"}]} I can get the Line and Column value as below var obj = $.parseJSON(data); var line = obj['line']; var column=obj['column']; I have tried below format but i cannt get response value: var res = new Array(obj['response']); alert(res[0]['criteria']); And var jsonObject = $.parseJSON(obj['response']); var innerArray = jsonObject['response']; alert(innerArray[0].fieldvalue); how can i get the values in obj['response'] ? Any one could help me to find the solution
Why do you need to create a new array? var res = new Array(obj['response']); alert(res[0]['criteria']); It is already converted to an array after you parse the JSON string, so this will work: var res = obj['response']; alert(res[0]['criteria']);