I have a json structure as:
{
"TestCaseList": [
{
"TC_1": {
"name":"verifyloginpagedetails",
"value":"2"
},
"TC_2": {
"name":"verify registration page details",
"value":"3"
}
}
],
"Summary": {
"v":[
{
"name":"over the ear headphones - white/purple",
"value":1
}
]
}
}
How to extract the values name, value of TC_1 , TC_2 where TC_1 is dynamic i.e. key of TestCaseList?
You can use the Object.keys method to get an array of the keys of an object.
With a single object in the array at "TestCaseList" in your JSON object, this will work:
// jsonObj is your JSON
testCaseKeys = Object.keys(jsonObj.TestCaseList[0]);
If, however, the array at "TestCaseList" contains more than one one element, you can use this to get each set of keys in an individual array:
testCaseKeySets = jsonObj.TestCaseList.map(obj => Object.keys(obj));
I'm sure a more elegant solution exists, but this will do the trick.
var myObj = {
"TestCaseList":
[{
"TC_1":
{"name":"verifyloginpagedetails",
"value":"2"},
"TC_2":
{"name":"verify registration page details",
"value":"3"}
}],
"Summary":{
"v":[{"name":"over the ear headphones - white/purple","value":1}]
}
}
let testCaseListKeys = Object.keys(myObj.TestCaseList[0]);
for(i=0; i < testCaseListKeys.length; i++){
let tclKey = testCaseListKeys[i];
console.log(tclKey + "\'s name = " + myObj.TestCaseList[0][tclKey].name);
console.log(tclKey + "\'s value = " + myObj.TestCaseList[0][tclKey].value);
}
The console.logs are your output. The important values there are the myObj.TestCaseList[0][tclKey].name and the myObj.TestCaseList[0][tclKey].value
** UPDATE **
After answering the question Ananya asked how to do this same thing if the object had a different structure.
Updated Object:
var myObj2 = {
"TestCaseList":
[{
"TC_1":{
"name":"verifyloginpagedetails",
"value":"2"}
},
{
"TC_2":{
"name":"verify registration page details",
"value":"3" }
}],
"Summary":
{
"v":[ {"name":"over the ear headphones - white/purple","value":1} ]
}
}
Updated JavaScript:
for(x=0;x<myObj2.TestCaseList.length;x++) {
let testCaseListKeys = Object.keys(myObj2.TestCaseList[x]);
for(i=0; i < testCaseListKeys.length; i++){
let tclKey = testCaseListKeys[i];
//console.log(tclKey);
console.log(tclKey + "\'s name = " + myObj2.TestCaseList[x][tclKey].name);
console.log(tclKey + "\'s value = " + myObj2.TestCaseList[x][tclKey].value);
}
}
Related
nested json structure
Json Structure:
{
"id": "30080",
"dataelements": {
"Name": "abc",
},
"children": [
{
"id": "33024",
"dataelements": {
"Name": "a",
},
"children": [
{
"id": "33024",
"dataelements": {
"Name": "b"
},
"children": [
{
"id": "33024",
"dataelements": {
"Name": "z"
},
"children": []
}
]
}
]
},
{
"id": "4800",
"dataelements": {
"Name": "d"
},
"children": [
{
"id": "4800",
"dataelements": {
.........................
I have my nested json data as shown in the image. For every child object, I create a node model. A child object can have additional child objects inside it.
if (ele == "dataelements")
{
var categoryNode = new NodeModel(
{
label: row.dataelements.Name,
icons: [{ iconName: 'product'}],
grid: row[ele]
});
}
if(ele == "children")
{
var subCategoryNode;
var subCategoryIndex = 1;
for (var i = 0, len = row.children.length; i<len; i++)
{
subCategoryNode = new NodeModel(
{
label: row.children[i].dataelements.Name,
icons: [{
iconName: '3dpart' }],
grid: row.children[i].dataelements
});
categoryNode.addChild(subCategoryNode);
}
}
This code handles only one level of child nodes.
How do I check for the inner children when I don't know exactly how many child levels are nested inside?
A quick run down on recursive functions and a gotcha to look out for
Recursive functions are great for nested data
They call themselves for each iteration of the input until it hits a base case
They can be tricky to wrap your head around at first
Recursive functions can hit the call stack limit if used poorly or the input is monstrous in size
Look out for variables used in the recursive calls, use let keyword to tell javascript to set the variable in the current scope
The Solution
Let's assume your JSON has been validated and this is the structure in the example below.
If I want to iterate through all elements in the JSON, I want to use a recursive call to make it neat, and simple to debug and simple to build on.
Here is an example of iterating through your given example JSON to print out an exploded view.
How to use the below code
Copy the recursiveSearch function
Call the recursiveSearch function passing in your JSON
Modify it to your needs, I gave you something to build on
CODE
var someJson = {"id": "30080","dataelements": {"Name": "abc"},"children": [{"id": "33024","dataelements": {"Name": "a"},"children": [{"id": "33024","dataelements": {"Name": "b"},"children": [{"id": "33024","dataelements": {"Name": "z"},"children": []}]}]}, {"id": "4800","dataelements": {"Name": "d"},"children": []}]};
//we set level to 0 (optional variable) this means we can omit it in the inital call for neat code
function recursiveScan(json, level=0)
{
//we store all of the output in a log and keep a track of the level to determine indenting
var log = "";
var indent = "";
//based on the current level of the recursion, we indent the text to make it readable
for (let i=0; i<level; i++)
{
indent += " ";
}
//avoid any bad json or invalid data by checking if the name and id is null
if(json.dataelements.Name != null && json.id != null)
{
//we know there is a valid element, write the name and id
log += indent + "ID: " + json.id + "<br>";
log += indent + "Name: " + json.dataelements.Name + "<br>";
//if there is any children
if(json.children.length > 0)
{
//just for neatness, lets draw the paranthesis
log += indent + "{" + "<br>";
//increase the level
level++;
//for each child, recursively call this function to get the next level of children if available
for(let t=0; t<json.children.length; t++)
{
log += recursiveScan(json.children[t], level);
}
//we are dropping our recursion level now, getting ready to return;
level--;
//close the paranthesis for neatness
log += indent + "}" + "<br>";
}
}
//return the final log
return log;
}
//now lets test the code
document.write(recursiveScan(someJson));
The above code produces
ID: 30080
Name: abc
{
ID: 33024
Name: a
{
ID: 33024
Name: b
{
ID: 33024
Name: z
}
}
ID: 4800
Name: d
}
Now a simple run-down without all the noise
function recursiveScan(json)
{
if(json.dataelements.Name != null && json.id != null)
{
//here you have access to id and dataelements
if(json.children.length > 0)
{
for(let t=0; t<json.children.length; t++)
{
//here you have access to each child as json.children[t]
//you could do the logic for the current child
//then pass the current child to the recursive function
recursiveScan(json.children[t]);
}
}
}
return true;
}
I am trying to compare two array of object values based on the specific key. Two object has same keys based on that i have to check whether the values are equal or not. One array is actual JSON object and the second one is test data, we have to verify the test data with JSON object and moreover if the test data value is same, it might have some extra space we need to trim that value as well.
var actualObject= [
{
"q1": "componentWillMount"
},
{
"q2": "willComponentUpdate"
},
{
"q3": "setState"
},
{
"q4": "componentUpdated"
}
]
Var testData =[
{q1: "componentWillMount"},
{q2: "willComponentUpdate"},
{q3: " PropTypes"},
{q4: "componentDidMount"}]
I will get the testData values from the Html code, on selection of radio buttons. Now i need to check how many answer are correct with actual JSON.
JS Code for it:
var marks= 0;
var wrong = 0;
for(var k =0 ; k<actualObject.length;k++){
if(JSON.stringify(actualObject[k]) == JSON.stringify(testData[k])){
marks++;
}
else {
wrong++;
}
}
var actualObject = [{
"q1": "componentWillMount"
},
{
"q2": "willComponentUpdate"
},
{
"q3": "setState"
},
{
"q4": "componentUpdated"
}
]
var testData = [{
q1: "componentWillMount"
},
{
q2: "willComponentUpdate"
},
{
q3: " PropTypes"
},
{
q4: "componentDidMount"
}
];
var marks = 0;
var wrong = 0;
for (var k = 0; k < actualObject.length; k++) {
if (JSON.stringify(actualObject[k]) == JSON.stringify(testData[k])) {
marks++;
} else {
wrong++;
}
}
console.log(marks, wrong);
Actually i would like to take value from each key and compare it with the actualObject.
If I understand correctly something like this should work:
Object.entries(testData).forEach(function (entry) {
if (actualObject[entry[0]] === entry[1].trim()) {
//answers match
} else {
//answers don't match
}
});
If you need to compare regardless of case then change entry[1].trim() to entry[1].trim().toLowerCase().
EDIT:
Just to remind you that maybe you should add a check whether or not the values in the test data are null/undefined, if they are strings or not, etc.
var user_business_data =[
{
"user_id":"5db3e3b1",
"blog":{
"blog_id":"128c522e"
},
"business_units":[
{
"business_unit_id":"000396c9",
"viewing":101
},
{
"business_unit_id":"01821e44",
"viewing":102
},
{
"business_unit_id":"02cbcad5",
"viewing":103
}
]
}
]
I want to get all the "business_unit_id" and store in a varible. for this i need get all the "business_unit_id". so i tried to print all the id's with the below code but i was unable to print.
if (undefined !== user_business_data.business_units && user_business_data.business_units.length) {
for(var i=0;i<user_business_data.business_units.length;i++){
var key = user_business_data.business_units[i];
console.log("Key : "+key, "Values : "+user_business_data.business_units[key]);
}
} else {
console.log("Undefined value");
}
There always i am getting undefined value.
var user_business_data=[{"user_id":"5db3e3b1","blog":{"blog_id":"128c522e"},"business_units":[{"business_unit_id":"000396c9","viewing":101},{"business_unit_id":"01821e44","viewing":102},{"business_unit_id":"02cbcad5","viewing":103}]}]
var unit_ids = [];
user_business_data.forEach(function(user) {
user.business_units.forEach(function(business) {
unit_ids.push(business.business_unit_id);
});
});
console.log(unit_ids);
user_business_data is an array, not an object, so you either need to loop through it or read a specific index from it.
Also, key in your code will be an object (a single business unit object), so you can't print it directly - instead you need to fetch a specific property within the object.
Here's a simple demo reading the first key from the outer array and then listing all the specific properties from the business units. The code can be simplified further potentially, but this illustrates the point:
var user_business_data =
[{
"user_id": "5db3e3b1",
"blog": {
"blog_id": "128c522e"
},
"business_units": [{
"business_unit_id": "000396c9",
"viewing": 101
},
{
"business_unit_id": "01821e44",
"viewing": 102
},
{
"business_unit_id": "02cbcad5",
"viewing": 103
}
]
}]
if (undefined !== user_business_data[0].business_units && user_business_data[0].business_units.length) {
for (var i = 0; i < user_business_data[0].business_units.length; i++) {
var key = user_business_data[0].business_units[i].business_unit_id;
console.log("Key : " + key, "Values : " + user_business_data[0].business_units[i].viewing);
}
} else {
console.log("Undefined value");
}
I suggest you get clear in your head the difference between arrays, objects and properties in JSON / JS objects, and then this kind of thing will become trivial.
user_business_data is an array and not an object.If you want to access any object from an array you have to specify the index as of which position you are referring.Therefore in your example change it to following to work:
if (undefined !== user_business_data[0].business_units && user_business_data[0].business_units.length) {
for(var i=0;i<user_business_data[0].business_units.length;i++){
var key = user_business_data[0].business_units[i]. business_unit_id;
console.log("Key : "+key, "Values : "+user_business_data[0].business_units[key]);
}
} else {
console.log("Undefined value");
}
It's because user_business_data is an array, not an object yet you access it like user_business_data.business_units instead of user_business_data[0].business_units
var user_business_data = [{"user_id": "5db3e3b1","blog": {"blog_id": "128c522e"}, "business_units": [{"business_unit_id": "000396c9","viewing": 101}, {"business_unit_id": "01821e44","viewing": 102},{"business_unit_id": "02cbcad5","viewing": 103}]}];
// Both methods give the same result, but the second checks for null values.
var ids1 = user_business_data[0].business_units.map(x => x.business_unit_id)
console.log('Method 1:', ids1);
// The && check for null values, kinda like an if statement.
var data = user_business_data.length && user_business_data[0]
var units = data && data.business_units
var ids2 = units && units.length && units.map(x => x.business_unit_id)
console.log('Method 2:', ids2)
If you want to print only the business_unit_ids then you can do as follows:
var user_business_data =
[
{
"user_id": "5db3e3b1",
"blog": {
"blog_id": "128c522e"
},
"business_units": [
{
"business_unit_id": "000396c9",
"viewing": 101
},
{
"business_unit_id": "01821e44",
"viewing": 102
},
{
"business_unit_id": "02cbcad5",
"viewing": 103
}
]
}
]
for(var i=0;i<user_business_data[0]["business_units"].length;i++){
console.log(user_business_data[0]["business_units"][i].business_unit_id)
}
Following is the JSON array, I want to get number of parent objects and then run for loop on them to get each object value.
It should give total count 2 as I have two parent objects - canvas0 and canvas1.
{"canvas0":
"{"objects":
[{"type":"textbox","originX":"left","originY":"top","left":40,"top":350,"width":200,"height":20.97,"fill":"black","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","text":"ADDRESScanvasPage1","fontSize":16,"fontWeight":"normal","fontFamily":"Helvetica","fontStyle":"","lineHeight":1.16,"textDecoration":"","textAlign":"center","textBackgroundColor":"","styles":{},"minWidth":20}],"background":""}"
,"canvas1":"{"objects":[{"type":"textbox","originX":"left","originY":"top","left":40,"top":350,"width":200,"height":20.97,"fill":"black","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","text":"ADDRESScanvasPage2","fontSize":16,"fontWeight":"normal","fontFamily":"Helvetica","fontStyle":"","lineHeight":1.16,"textDecoration":"","textAlign":"center","textBackgroundColor":"","styles":{},"minWidth":20}],"background":""}"}
As everyone said, your JSON is invalid. The same valid JSON in minimal would look like the following.
var json = {
"canvas0": {
"objects": [
{
"type": "textbox",
"originX": "left"
}
],
"background": "#000"
},
"canvas1": {
"objects": [
{
"type": "select",
"originX": "right"
}
]
}
};
To be able to iterate thru, you don't need the count of top level keys. And moreover, a JSON object doesn't have the length property. All you need to do is, use for...in loops and display the key/values accordingly. Here is an example of how you can do it. Bear in mind that this only works with your specific case as the loop needs changing as and when the JSON levels change.
alert ( "Length of top level keys: " + Object.keys(json).length );
for (var key in json) {
var canvas = json[key];
for (var key2 in canvas) {
if (canvas[key2] instanceof Array) {
var object = canvas[key2][0];
for (var key3 in object) {
alert (key3 + ": " + object[key3]);
}
} else {
alert (key2 + ": " + canvas[key2]);
}
}
}
Here is a working demo with the complete code.
I have a function that will get a JSON array with objects. In the function I will be able to loop through the array, access a property and use that property. Like this:
Variable that I will pass to the function will look like this:
[{
"id": 28,
"Title": "Sweden"
}, {
"id": 56,
"Title": "USA"
}, {
"id": 89,
"Title": "England"
}]
function test(myJSON) {
// maybe parse my the JSON variable?
// and then I want to loop through it and access my IDs and my titles
}
Any suggestions how I can solve it?
This isn't a single JSON object. You have an array of JSON objects. You need to loop over array first and then access each object. Maybe the following kickoff example is helpful:
var arrayOfObjects = [{
"id": 28,
"Title": "Sweden"
}, {
"id": 56,
"Title": "USA"
}, {
"id": 89,
"Title": "England"
}];
for (var i = 0; i < arrayOfObjects.length; i++) {
var object = arrayOfObjects[i];
for (var property in object) {
alert('item ' + i + ': ' + property + '=' + object[property]);
}
// If property names are known beforehand, you can also just do e.g.
// alert(object.id + ',' + object.Title);
}
If the array of JSON objects is actually passed in as a plain vanilla string, then you would indeed need eval() here.
var string = '[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]';
var arrayOfObjects = eval(string);
// ...
To learn more about JSON, check MDN web docs: Working with JSON
.
This is your dataArray:
[
{
"id":28,
"Title":"Sweden"
},
{
"id":56,
"Title":"USA"
},
{
"id":89,
"Title":"England"
}
]
Then parseJson can be used:
$(jQuery.parseJSON(JSON.stringify(dataArray))).each(function() {
var ID = this.id;
var TITLE = this.Title;
});
By 'JSON array containing objects' I guess you mean a string containing JSON?
If so you can use the safe var myArray = JSON.parse(myJSON) method (either native or included using JSON2), or the usafe var myArray = eval("(" + myJSON + ")"). eval should normally be avoided, but if you are certain that the content is safe, then there is no problem.
After that you just iterate over the array as normal.
for (var i = 0; i < myArray.length; i++) {
alert(myArray[i].Title);
}
Your question feels a little incomplete, but I think what you're looking for is a way of making your JSON accessible to your code:
if you have the JSON string as above then you'd just need to do this
var jsonObj = eval('[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]');
then you can access these vars with something like jsonObj[0].id etc
Let me know if that's not what you were getting at and I'll try to help.
M
#Swapnil Godambe
It works for me if JSON.stringfy is removed.
That is:
$(jQuery.parseJSON(dataArray)).each(function() {
var ID = this.id;
var TITLE = this.Title;
});
var datas = [{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}];
document.writeln("<table border = '1' width = 100 >");
document.writeln("<tr><td>No Id</td><td>Title</td></tr>");
for(var i=0;i<datas.length;i++){
document.writeln("<tr><td>"+datas[i].id+"</td><td>"+datas[i].Title+"</td></tr>");
}
document.writeln("</table>");