I have this multiple set of data as array
data = [{"id": "1", "name" : "abc", "key1" : "value12 }, {"id": "2", "name" : "cde", "key2" : "value2" }.....]
I need to get this data using jQuery
json = $.parseJSON(data);
but how do I access the parsed JSON data? json.id shows the result as undefined.
Thanks
Update : Sorry I fixed the above example JSON I gave, I just quickly typed it by myself and it's not the original json I'm having trouble with. I just gave it to give an idea about the problem that I was having. Thanks for the answers :)
It isn't JSON. It isn't even JavaScript.
If you fix the syntax errors (such as the missing quotes and the missing commas between the array items) then it is an array literal (containing object literals which contain …). Don't parseJSON it (you use that on JSON texts stored in JavaScript strings).
Since it is an array. It doesn't have an id. It has a number of numerical indexes.
var someObject = data[0];
The objects stored on those indexes have ids.
var id = someObject.id;
Your json is invalid. ',' are missing between objects.
Suppose if json is :
data = [{"id": "1", "name" : "abc", "key1" : "value12" }, {"id": "2", "name" : "cde", "key2" : "value2" }]
Then you can access 'id' element using this :
data[0].id
Try this:
var data = '{"id": "1", "name" : "abc", "key1" : "value12" } , {"id": "2", "name" : "cde", "key2" : "value2"}';
var obj = JSON.parse('[' + data + ']');
alert(obj[0].id);
Here is demo
Your json is invalid,
data = [{"id": "1", "name" : "abc", "key1" : "value12" }, {"id": "2", "name" : "cde", "key2" : "value2" }.....]
Retrive using:
var id = data[0].id;
console.log(id);
Related
I have a Set of userId strings like this:
["1", "2", "3"....]
I want to loop over and convert them to this object format:
[
{
"field" : "tag",
"key" : "user_id",
"relation": "=",
"value" : 1 // Insert userId here
},
etc...
]
And then in between the userId objects I need to add the object:
{ "operator": "OR" }
So the array looks like
[{userId object}, {OR object}, {userId object}, etc...]
There are an arbitrary number of userIds - there could be thousands. What I need to happen is, if there are more than 200 objects in the array, I need to call the function sendObjects(array) with that object array, and then reset the array and continue from where I left off. The array must not end in an OR object. How to do this??
Use forEach() on array numArray:
var numArray = ["1", "2", "3"];
var res = [];
numArray.forEach(function(val, index){
var obj = {
"field" : "tag",
"key" : "user_id",
"relation": "=",
"value" : val
};
res.push(obj);
if(numArray.length-1 !== index){
res.push({ "operator": "OR" });
}
});
console.log(res);
I have an array of hundreds of objects like the following:
{
"id" : 893,
....
"responsable" : {
"id" : 792,
"version" : 13,
"username" : "xxxxx#mail.es",
"nombre" : "Peter",
"apellido1" : "Murphy",
"apellido2" : "XXX"
}
}
Within my object I have another object that is "responsable". Need to get an array of these objects, but only once each (remove repeated)
You can use reduce to summarise your data to an object. And use Object.values to convert the object into an array.
This example will extract all responsables and remove duplicates based on responsable.id
let arr = [
{"id": 893,"responsable": {"id": 792,"version": 13,"username": "xxxxx#mail.es","nombre": "John","apellido1": "Murphy","apellido2": "XXX"}},
{"id": 894,"responsable": {"id": 793,"version": 13,"username": "xxxxx#mail.es","nombre": "Peter","apellido1": "Murphy","apellido2": "XXX"}},
{"id": 895,"responsable": {"id": 792,"version": 13,"username": "xxxxx#mail.es","nombre": "John","apellido1": "Murphy","apellido2": "XXX"}},
{"id": 896,"responsable": {"id": 794,"version": 13,"username": "xxxxx#mail.es","nombre": "Paul","apellido1": "Murphy","apellido2": "XXX"}}
];
let responsable = Object.values(arr.reduce((c, v) => Object.assign(c, {[v.responsable.id]: v.responsable}), {}));
console.log(responsable);
Update: I'm trying to construct a table out of my data structure(ie section1) and then allow users to add rows to the table to insert more rows and save them to my datastructure.
I have an array newArr in the form of key value pairs. When somebody clicks a button, I want to be able to push the newArray into the Groups.I dont seem to be able to push into the Groups array. Chrome dev tools shows Groups as Objects and i'm not certain how to loop through and append to each item of the Groups Object. Feel free to modify the $scope.section1 to a different datastructure that could make it easier to push new items to it.
$scope.section1 = [{
"id":1, "Name": "Section 1: Inventory",
"Groups":[
{"cell" : "Number", "cellValue" : "value1"},
{"cell" : "Location", "cellValue" : "value2"},
{"cell" : "Severity", "cellValue" : "value3"}
],
"FootNotes":[
{"templateurl" : "components/section/templates/notes/section1.notes.html"}
]
}]
var newArr = {"cellValue" : "value4","cellValue" : "value5","cellValue" : "value6"}
So the output should look like
$scope.section1 = [{
"id":1, "Name": "Section 1: Inventory",
"Groups":[
{"cell" : "Number", "cellValue" : "value1", "cellValue" : "value4"},
{"cell" : "Location", "cellValue" : "value2", "cellValue" : "value5"},
{"cell" : "Severity", "cellValue" : "value3", "cellValue" : "value6"}
],
"FootNotes":[
{"templateurl" : "components/section/templates/notes/section1.notes.html"}
]
}]
You can't have two properties with the same name. You have cellValue two times for Object group. What are you trying to do?
You'd better like to change the structure itself:
...
"Groups": [
{
name: "group1",
values: ['value 1', 'value 2', 'value 3']
}
]
Then, adding one more value to group1:
$scope.section1[0].Groups.values.push('value 4')
Note than I'm trying to respect the whole structure that you already have, but I don't meaning this is the optimal way to solve your problem
$scope.section1[0].Groups.push({
"cell" : "Number",
"cellValue" : "value1",
"anotherCellValue" : "value4"
});
Below is my json structure. On success of collection.fetch() i'm looping through the structure.
Currently i use
this.collection.each(function(model) { .. }
How do i obtain key name like plants, animals and instead loop using the names.
JSON
var jsonObj = { // 0 - recommended , 1 - New
"plants" : [
{
"title" : "title1",
"desc": "description.."
},
{
"title" : "titl2",
"desc": "description."
}
],
"animals" : [
{
"title" : "title1",
"desc": "description.."
},
{
"title" : "titl2",
"desc": "description."
}
]
};
Snapshot of collection
This would work, but you'd use a normal for loop, not "each":
for(i in jsonObj){
alert(i);
}
here is a fjsfiddle: http://jsfiddle.net/r5nwP/
Is that what you're after?
You can use the underscore keys to get a list of names:
var thenames =_.keys(yourobject);
In this case thenames will contain a list of the keys you are looking for. Here is the documentation for it:
http://underscorejs.org/#keys
keys_.keys(object)
Retrieve all the names of the object's properties.
_.keys({one : 1, two : 2, three : 3});
=> ["one", "two", "three"]
I have the following JSON output and I need to access the variable TN_TEXTO. How can I access it?
["data", [
["notification",
{
"TN_CODIGO": "3",
"TN_TP_CODIGO": "1",
"TN_TEXTO": "dddddddddddd",
"TN_DATA": "1325708743",
"TN_LINK": "",
"TN_READ": "0"
}]
]]
Thanks in advance!
Use JSON.parse to convert a string to an object. Then you can use JavaScript syntax on it.
var object = JSON.parse(string);
alert(obj[1][0][1].TN_TEXTO);
Live example
myvar = ["data", [
["notification",
{
"TN_CODIGO": "3",
"TN_TP_CODIGO": "1",
"TN_TEXTO": "dddddddddddd",
"TN_DATA": "1325708743",
"TN_LINK": "",
"TN_READ": "0"
}]
]]
alert(myvar[1][1]["TN_CODIGO"]); // is 3
If you're using JSON string:
var json = '["data", [["notification", { "TN_CODIGO": "3", "TN_TP_CODIGO": "1", "TN_TEXTO": "dddddddddddd", "TN_DATA": "1325708743", "TN_LINK": "", "TN_READ": "0" }] ]]';
var array = JSON.parse(json);
alert(array[1][0][1]["TN_TEXTO"]);
If you have already parsed the string just:
alert(array[1][0][1]["TN_TEXTO"]);