This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I need to fetch the values from this JSON in my java script:
[{
"selectionName": "Select",
"subSelections": [{
"id": 4,
"subSelectionName": "Select",
"description": "Deepmala"
}
]
}, {
"selectionName": "week14",
"subSelections": [{
"id": 7,
"subSelectionName": "1",
"description": ""
}
]
}, {
"selectionName": "test",
"subSelections": [{
"id": 6,
"subSelectionName": "test",
"description": ""
}
]
}, {
"selectionName": "select",
"subSelections": [{
"id": 3,
"subSelectionName": "sub-select",
"description": "Created by Prakash"
}
]
}, {
"selectionName": "testcreate",
"subSelections": [{
"id": 1,
"subSelectionName": "testcreate",
"description": ""
}
]
}, {
"selectionName": "by htmlwidget",
"subSelections": [{
"id": 5,
"subSelectionName": "by htmlwidget",
"description": "created by html widget"
}
]
}
]
Any suggestions?
You could use something like JSONSelect to extract certain values.
http://jsonselect.org/
Here's an example of how to use it:
(found in this JSFiddle)
$(function(){
/*
Json as easy as SQL ??? RT #lloydhilaiel JSONSelect - CSS-like selectors for JSON - http://jsonselect.org
Testing...
*/
var jsonData = {
"name": {
"first": "Lloyd",
"last": "Hilaiel"
},
"favoriteColor": "yellow",
"languagesSpoken": [
{
"language": "Bulgarian",
"level": 2},
{
"language": "English",
"level": 1},
{
"language": "Spanish",
"level": 7}
]
};
var selector = '.name > *'; // xPath CSS like selector
try {
var resultObj = JSONSelect.match(selector, jsonData);
console.log(typeof resultObj);
console.log(resultObj);
console.log('- - - - -');
JSONSelect.forEach(selector, jsonData, function(resultObj) {
console.log(typeof resultObj);
console.log(resultObj);
console.log('- - - - -');
$('body').append('<p>' + $.trim(JSON.stringify(resultObj, null, ' ')) + '</p>');
});
} catch(e) { console.log(e); }
});
JSON objects are easy to handle
var JSON = //Your JSON Object
JSON[0].selectName //returns 'Select'
JSON[0].subSelections[0].id //returns 4
and so on.
Any array objects can be treated as arrays. Any mapped objects can be returned by using the key like a field name for the JSON object.
Related
I have the following array
[ { "id": 1, "name": "Test" }, { "id": 2, "name": "Test2" }
How can I convert that to
[ { "id": '1', "name": "Test" }, { "id": '2', "name": "Test2" }
For all the objects in an array, you want to convert a property type from number to string. You can use Array.forEach to achieve the same.
let arr = [ { "id": 1, "name": "Test" }, { "id": 2, "name": "Test2" }];
arr.forEach(v => v.id += '');
console.log(arr);
Is there any plugin that allow to dynamically build following data structure in plain JavaScript object or JSON?
This is the structure I would like to implement:
Check my plunker (https://plnkr.co/edit/1eWjXLPumuOMhv8BjaNw?p=preview)
{
"name": "Select0",
"id": "select0",
"values": [
"Option1",
"Option2"
],
"dependent": {
"name": "Select1",
"id": "select1",
"values": {
"Option1": [
"Option11",
"Option12"
],
"Option2": [
"Option21",
"Option22"
]
},
"dependent": {
"name": "Select2",
"id": "select2",
"values": {
"Option1": {
"Option11": [
"Option111",
"Option112"
],
"Option12": [
"Option121",
"Option122"
]
},
"Option2": {
"Option21": [
"Option211",
"Option212"
],
"Option22": [
"Option221",
"Option222"
]
}
},
"dependent": []
}
}
}
Note that you can set values on properties on an object and then print it as a JSON.
var obj = {};
object.name = "Select0";
// Set more properities.
// Stringify object to JSON format looking as the one in the question.
var json = JSON.stringify(obj, null, 2);
// Verify by printing on the screen.
console.log(json);
This is a follow up question for Compare two arrays and update with the new values by keeping the existing objects using javascript which was ansewered by #Siderite Zackwehdex
Please check this plunker I'm getting the deleted Object in the changedArray1.Is there any option to fix this.Its working fine for other changes like add,update but not for delete.
One feature that I tried to implement is to add an additional object ie., "removedIds":[23] , that holds the deleted id's as an array in each level of objects and identifying them in the evaluation process.. but didn't find the right soultion
Example data:
var parentArray1=[
{
"id": 1,
"name": "test",
"context": [
{
"operationalContextId": 1.1,
"valueChainEntityDetails": [
{
"valuChainEntityId": 3,
"name": "test",
"context": [
{
"id": 3.1,
"name": "test 3.1",
"activityDetails": [
{
"activityId": 22,
"name": "test 3.1"
},
{ //trying to remove activity id 23
"activityId": 23,
"name": "changed test 23"
}
]
}
]
}
]
}
]
}
]
var changedArray1=[
{
"id": 1,
"name": "test2",
"context": [
{
"operationalContextId": 1.1,
"valueChainEntityDetails": [
{
"valuChainEntityId": 3,
"name": "changed test3",
"context": [
{
"id": 3.1,
"name": "test 3.1",
"removedIds":[23] ,
"activityDetails": [ //activity id 23 is removed in this JSON but reflecting in parentArray1
{
"activityId": 22,
"name": "changed test 3.1"
}
]
}
]
}
]
}
]
}
]
Here is the my first JSON Array format...
[
{
"id": "1234",
"caption": "caption1"
},
{
"id": "2345",
"caption": "caption2"
},
{
"id": "3456",
"caption": "caption3"
}
]
and here is another JSON Array Format
[
[
{
"id": "1234",
"value": "value11"
},
{
"id": "2345",
"value": "value12"
},
{
"id": "3456",
"value": "value13"
}
],
[
{
"id": "1234",
"value": "value21"
},
{
"id": "2345",
"value": "value22"
},
{
"id": "3456",
"value": "value23"
}
]
]
The above mentioned Two JSON Arrays, i need to compare each one with Id and need to format a new JSON Array with caption and value using javascript.
[
[
{
"caption" : "caption1",
"value":"value11"
},
{
"caption" : "caption2",
"value":"value12"
},
{
"caption" : "caption3",
"value":"value13"
}
],
[
{
"caption" : "caption1",
"value":"value21"
},
{
"caption" : "caption2",
"value":"value22"
},
{
"caption" : "caption3",
"value":"value23"
}
]
]
Please help me out.
You can do it in many ways. Below I show two variants:
Option 1: Pure JavaScript
In this example the program preindex first array for faster access to it data, and then loops over second array with map() function to create new array of arrays:
// Create index version of first array
var aix = {};
for(var i=0;i<arr1.length;i++) {
aix[arr1[i].id] = arr1[i].caption;
}
// Loop over array of arrays
var res1 = arr2.map(function(arr22){
return arr22.map(function(a){
return {caption:aix[a.id], value:a.value};
}
});
Option 2: Using special SQL library (Alasql)
Here, you can JOIN to arrays automatically with special SQL statement:
var res2 = arr2.map(function(a){
return alasql('SELECT arr1.caption, a.[value] \
FROM ? a JOIN ? arr1 USING id',[a,arr1]);
});
You can try these variants in working snippet below or play with it in jsFiddle.
(Disclaimer: I am the author of Alasql)
var arr1 = [
{
"id": "1234",
"caption": "caption1"
},
{
"id": "2345",
"caption": "caption2"
},
{
"id": "3456",
"caption": "caption3"
}
];
var arr2 = [
[
{
"id": "1234",
"value": "value11"
},
{
"id": "2345",
"value": "value12"
},
{
"id": "3456",
"value": "value13"
}
],
[
{
"id": "1234",
"value": "value21"
},
{
"id": "2345",
"value": "value22"
},
{
"id": "3456",
"value": "value23"
}
]
];
// JavaScript version
var aix = {};
for(var i=0;i<arr1.length;i++) {
aix[arr1[i].id] = arr1[i].caption;
}
var res1 = arr2.map(function(arr22){
return arr22.map(function(a){
return {caption:aix[a.id], value:a.value};
});
});
document.getElementById("res1").textContent = JSON.stringify(res1);
// Alasql version
var res2 = arr2.map(function(a){
return alasql('SELECT arr1.caption, a.[value] FROM ? a JOIN ? arr1 USING id',[a,arr1]);
});
document.getElementById("res2").textContent = JSON.stringify(res2);
<script src="http://alasql.org/console/alasql.min.js"></script>
<p>Varian 1: JavaScript</p>
<div id="res1"></div>
<p>Variant 2: Alasql</p>
<div id="res2"></div>
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
i have this JSON Object, and i wanna access to this > object.foda.forta.id or name.. in JAVASCRIPT
thanks
note: this json it's created by xml2js.Parser()
{
"object": {
"foda": [
{
"forta": [
{
"id": [
"1"
],
"name": [
"dasdghjg"
]
},
{
"id": [
"2"
],
"name": [
"jj"
]
},
{
"id": [
"3"
],
"name": [
"gjhjg"
]
}
]
}
]
}
}
You cant access by object.foda.forta.id
as foda and fotra are lists, you can access by object.foda[0].forta[0].id
Note - 0 is used for sample only you can use any index (less the size of array)
Your JSON was terribly created, lots of unnecessary Arrays, but you can access it like this:
var obj = {
"object": {
"foda": [
{
"forta": [
{
"id": ["1"],
"name": ["dasdghjg"]
},
{
"id": ["2"],
"name": ["jj"]
},
{
"id": ["3"],
"name": ["gjhjg"]
}
]
}
]
}
};
document.body.innerHTML = "ID: " + obj.object.foda[0].forta[0].id[0] + " - Name: " + obj.object.foda[0].forta[0].name[0];
Take a look at this fiddle!