Get the Inner Array Value of JSON in Javascript - 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']);

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 parse a json data with multiple values?

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.

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

JSON Data to Javascript Array Undefined

I am trying to get data from a JSON file and use Javascript Code to put it into Google Spreadsheet, I did good with some part of the data but I'm stuck with the other part, where I want to match the User Id and put all the data with it on a same row,
JSON Data:
"m":{"414":{"a":{"0":{"c":38,"p":12812.4},
"4":{"c":35,"p":10559.94},"2":{"c":43,"p":35811.63},
"6":{"c":48,"p":45530}},"d":{"0":{"c":55,"p":5477.06225},
"4":{"c":694,"p":106649.473},"2":{"c":1844,"p":716733.50775000011},
"6":{"c":605,"p":324152.5875}},"i":{"0":{"c":0,"p":0},
"4":{"c":0,"p":0},"2":{"c":0,"p":0},"6":{"c":542,"p":19893.93}}},
"404":{"a":{"0":{"c":15,"p":916.182},"4":{"c":50,"p":12357},
"2":{"c":530,"p":390825.27},"6":{"c":58,"p":4841.55}},
"d":{"0":{"c":10,"p":3145.8},"4":{"c":770,"p":141876.12},
"2":{"c":4854,"p":2173966.6125000003},
"6":{"c":1973,"p":1145077.425}},"i":{"0":{"c":0,"p":0},
"4":{"c":0,"p":0},"2":{"c":0,"p":0},"6":{"c":594,"p":25444.41}}}},
Javascript:
var testUF = [];
var Uid = Object.getOwnPropertyNames(doc1.m);
for (var lp2 = 0; lp2 < Uid.length; lp2++) {
var Ua1 = doc1.m[lp2].a["0"].p;
var Ua2 = doc1.m[lp2].a["4"].p;
var Ua3 = doc1.m[lp2].a["2"].p;
var Ua4 = doc1.m[lp2].a["6"].p;
var Ud1 = doc1.m[lp2].d["0"].p;
var Ud2 = doc1.m[lp2].d["4"].p;
var Ud3 = doc1.m[lp2].d["2"].p;
var Ud4 = doc1.m[lp2].d["6"].p;
var Ui4 = doc1.m[lp2].i["6"].p;
testUF.push([Uid,Ua1,Ua2,Ua3,Ua4,Ud1,Ud2,Ud3,Ud4,Ui4]);}
I am getting the Array on the Uid while Debugging, but all the other Variables don't get the data it stays Undefined. I want all the other variables to match with the Uid's and stay in the same row. I did the JSON parsing and everything.
I am asking for the first time on stackoverflow, please forgive me if I couldn't state everything properly. Thank you for the help. :)
You're not using the array index for Uid properly.
Change:
var Ua1 = doc1.m[lp2].a["0"].p;
To:
var Ua1 = doc1.m[Uid[lp2]].a["0"].p;

How to convert the list of array to list of json object

i like to convert array to json object like this
var obj = [{item:'name1',start:new date()}, {item:'name2',start:new date()},{item:'name3',start:new date()}]
i am using single dimension array means is working fine. check this link http://jsfiddle.net/H4ezf/1/
var objectArray= {};
objectArray['title']='All Day Event';
objectArray['start']=new Date(y, m, 1);
console.log(JSON.stringify(objectArray));
output as : {"title":"All Day Event","start":"2012-06-30T18:30:00.000Z"}
but i try to convert list of array to list json object using json stringify like this
var objectArray= {};
objectArray[0]['title']='name1';
objectArray[0]['start']=new Date();
objectArray[1]['title']='name2';
objectArray[1]['start']=new Date();
console.log(JSON.stringify(objectArray));
it not working. what i am wrong here.
Please any one can help me to solve this problem
You cannot do this:
var objectArray= {};
objectArray[0]['title']='name1';
as objectArray[0] does not exist yet. There is no array at that index and therefor you cannot add a string at an index. You have do declare the array first. The rest of your code works just fine.
JSFIDDLE
var objectArray= [];
objectArray[0] = {}
objectArray[0]['title']='name1';
objectArray[0]['start']=new Date();
objectArray[1] = {}
objectArray[1]['title']='name2';
objectArray[1]['start']=new Date();
console.log(JSON.stringify(objectArray));​
try it like this:
var objectArray = [];
objectArray[0] = {};
objectArray[0]['title'] = 'name1';
objectArray[0]['start'] = new Date();

Categories