I've got a JSON string and I can't seem to extract the value from the amount attribute
var jsonString =[{"id":null,"recordtype":null,"columns":{"amount":1049.849}}]
I've tried accessing amount using:
jsonString[0].columns[0].amount
and also tried using:
jsonString.columns.amount
but all seem to generate an error.
It is
jsonString[0].columns.amount
Because the value under "columns" is immediately an object, not an array.
jsonString[0].columns.amount
is the way to go. Columns is not an array
Try this,
jsonString[0].columns.amount;
You have an array, then just objects. So the answer is jsonString[0].columns.amount
It's useful to indent it, if in doubt:
[{
"id": null,
"recordtype": null,
"columns": {
"amount": 1049.849
}
}]
You have to create the JSON object like
var jsonString =[
{
"id":null,
"recordtype":null,
columns:
{
"amount":1049.849
}
}
]
Then you can access with jsonString[0]['columns']['amount'];
Here is the jfiddle link,
https://jsfiddle.net/hhLahiru/n0de3Lc8/
i can do on console
var jsonString =[{"id":null,"recordtype":null,"columns":{"amount":1049.849}}];
jsonString[0]
Object {id: null, recordtype: null, columns: Object}
jsonString[0].columns
Object {amount: 1049.849}
jsonString[0].columns.amount
1049.849
Related
I need help formatting this response. Below is a response that I'm getting from a server. I'd like to write out a static version of this but I'm struggling with the formatting of how to write it.
Response I want to write:
[
category1: [{...},{...}],
category2: [{...},{...}],
category3: [{...},{...}]
]
My attempt:
const myArr = [
"category1": [{...},{...}],
"category2": [{...},{...}],
"category3": [{...},{...}]
]
What is it that I'm doing wrong here? Thank you! Apologises for the n00b question.
You've written something that's a cross between an array literal and an object literal. It needs to be one or the other.
An array literal, containing arrays of objects:
const myArr = [
[{/*...*/},{/*...*/}],
[{/*...*/},{/*...*/}],
[{/*...*/},{/*...*/}]
];
You access the "category1" array via myArr[0], "category2" via myArr[1], etc.
An object literal, containing arrays of objects as the values of properties:
const myObj = {
category1: [{/*...*/},{/*...*/}],
category2: [{/*...*/},{/*...*/}],
category3: [{/*...*/},{/*...*/}]
};
You access the "category1" array via myObj.category1, "category2" via myObj.category2, etc.
Although it's possible to give arrays arbitrary named properties (because arrays are objects), you can't do so in an array literal (you have to create the array first, then add the properties), and you usually don't want to do it anyway. :-)
You can only use keys in an object not in an array. Try like this:
const myArr = {
"category1": [{...},{...}],
"category2": [{...},{...}],
"category3": [{...},{...}]
};
Something is wrong from that request because there is no result format like that. Make sure you have the correct response.
Object version:
const myArr = {
"category1": [{...},{...}],
"category2": [{...},{...}],
"category3": [{...},{...}]
};
console.log(Object.keys(myArr).map((key) => myArr[key]));
when my ajax call completes an array of json is returned
for my angular data binding to work perfectly, i need to merge all values in to a single JSON file. I have tried $.extend(), it's giving following output
Need a solution for this
for example if my response looks like this:
[0:"{'test':'test'}", 1:"{'test':'test'}", 2:"{'test':'test'}",3: "{'test':'test'}"];
the output i need is :
{ test':'test', 'test':'test', 'test':'test', 'test':'test' }
Edit:
The final value will be associated to the ng-model automatically.
desired output example:
{
"unique_id": 172,
"portfolio": "DIGITAL",
"bus_unit": "dummy",
"project_phase": "",
"test_phase": "SIT",
"project": "Google",
"golivedate": "03/09/2016",
"performance": "Green",
"summary": "jgnbfklgnflknflk",
"last_updated": "",
"risks_issues": "gfmngfnfglkj",
"project_start": "03/16/2016",
"batchLast_run": "",
"custom_project": "1",
"test_execution_id": 5456,
"unique_id": 172,
"test_execution_id": 5456,
"pass": 8,
"fail": 8,
"blocked": 8,
"in_progress": 8,
"no_run": 8,
"not_available": 0,
"total": 8
}
From what I understand you are trying to convert array of Json data into one singel json data. So you have array of values but you would want all of them in one variable. Try this
var testData = ["{'test':'test'}", "{'test':'test'}", "{'test':'test'}", "{'test':'test'}"];
var finalData ="";
$.each(testData,function(index,value){
finalData += value +',';
});
finalData = finalData.replace(/\},\{/g,',').slice(0, -1);
document.write(finalData);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Map just applies the function to every element in the array.
var arrayOfJSON = ...
var arrayOfObjects = arrayOfJSON.map(function (jsonString){
return JSON.parse(jsonString)
})
var jsonStringWithAllObjects = JSON.stringify(arrayOfObjects)
If you use underscore.js then can easily
like:
var list = [{"test1": "test1"}, {"test2": "test2"}, {"test3": "test3"}];
var newList = _.extend.apply(null,[{}].concat(list));
then output will be
{ test1: "test1", test2: "test2", test3: "test3" }
Iterate over the array, convert every value to a JSON object, concatenate and then convert to a string back.
If you need fo this more times, you probably should make this a function.
I have a case that is how to find the value of a key that is in sekrip as follows:
JSON.parse({
"data": [
{
"id_user": "351023",
"name": "",
"age": "29",
"link": "http://domain.com"
}
]
});
The above data was obtained from:
<script type='text/javascript' src='http://domain.com/target.php'></script>
I want to get the value of the key "id_user", anyone can help me?
Thanks before.
Several issues here. Firstly, the method you're looking for is JSON.parse(), not parseJSON. Secondly, what you're providing to that function is already an object, not a JSON string, therefore it doesn't need to be deserialised as you can access it as you would any normal object:
var obj = {
"data": [{
"id_user": "351023",
"name": "",
"age": "29",
"link": "http://domain.com"
}]
}
console.log(obj.data[0].id_user);
First its JSON.parse. Second it needs to be a JSON string.
Fiddle: http://jsfiddle.net/5m5qs1x4/
var jsonData = JSON.parse('{"data": [{"id_user": "351023","name": "","age": "29","link": "http://domain.com"}]}');
document.getElementById('test').textContent = jsonData.data[0].id_user;
You need to parse JSON string and than, access to keys/index
var data = JSON.parse("{\"data\": [{ \"id_user\": \"351023\",\"name\": \"\",\"age\": \"29\",\"link\": \"http://domain.com\"}]}");
document.write(data["data"][0]["id_user"]);
I am new to jQuery and json. Trying to draw a graph with reference to the demo from
http://www.highcharts.com/stock/demo/
This graph uses data from a json file
http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?
this file directly started with data. But the json file that I want to use looks something like this
{"name": {
"text1": "on",
"data": {
[1147651200000,67.79],
[1147737600000,64.98],
[1147824000000,65.26],
[1147910400000,63.18],
[1147996800000,64.51],
[1148256000000,63.38],
[1148342400000,63.15],
[1148428800000,63.34],
[1148515200000,64.33],
[1148601600000,63.55],
[1148947200000,61.22],
[1149033600000,59.77],
}
}}
could someone help me with this please
Your json is invalid.
If your data property is an array, so you should use [] to denote the array.
I'm assuming that array is an array of arrays, so inside [], it should have multiple []s.
Like:
[[x1,y1],[x2,y2]...]
Try formatting your code sample as follows...
{
"name": {
"text1": "on",
"data": [
[
1147651200000,
67.79
],
[
1147651200000,
67.79
]
]
}
}
Use something like http://jsonlint.com/ to check your json's validity.
The data which I fetch from PHP page is like:
[{
"id": "1",
"name": null,
"startdate": "2012-07-20",
"starttime": "09:53:02",
"enddate": "2012-07-20",
"endtime": "09:54:10",
"duration": "01:00:00",
"feedbacks": [{
"id": "1",
"type": "1",
"content": "cont"
}],
"conditions": [{
"id": "1",
"dev_id": "1",
"mod_id": "2",
"sub_id": "3",
"to_be_compared_value": "1",
"comparison_type": "1"
}],
"actions": [{
"id": "1",
"dev_id": "1",
"mod_id": "1",
"sub_id": "1",
"target_action": "1"
}]
}]
Which way is easy, efficent and elegant to traverse this object? I used this two until this time. Can you tell me which one must be my choice, or can you give me an alternative? And why? I have a running version of my application and I'm reviewing now my own code, and I want to take some advices from you all.
Thanks in advance,
Methods I use before:
$.map
for(var i in obj)
One more to go, I will create a table from this data.
I would use jQuery's each() (or map() if I wanted to change the data)
I should add that you should also create a function which returns an object (possibly even with some utility methods), since your data isn't very JS-friendly right now. Those dates and times, those ID's as strings.
Example:
function cleanMyObject(object){
var cleanFeedbacks = function(feedbacks){
/* ... */
return feedback;
};
object.start = /* transform date and time strings to datetime object ...*/
object.end = /*...*/
/*...*/
$.map(object.feedbacks,cleanFeedbacks);
/* cleanup the remaining objects... */
return object;
}
$.map(receivedData, cleanMyObject);
// cleanMyObject() returns the modified object so $.map will clean everything in your array.
I prefer to use http://underscorejs.org/ for things like this. It has a lot of useful functions for objects, collections etc.
If the data you are recieving doesn't change, just parse the object and use the keys you need.
All browsers I'm aware of have a function called JSON.parse to convert a JSON string into a JS object.
What I'm trying to say is: Don't be lazy, you aren't gaining any benefits from writing a "general" function if your object will always provide the same data, and there is little to no chance you can use that function again with a different object.
var myobj= JSON.parse(phpJSONstring);
var feedbacks= myobj["feedbacks"];
//do something with feedbacks
var conditions= myobj["conditions"];
//do something with conditions
etc
You can transform the json string in a javascript object, and then access the object like this:
var obj = jQuery.parseJSON(jsonString);
alert('Id='+obj.id);
var feedbackList = obj.feedbacks;
for (var i=0; i<feedbackList.length; i++) {
...
}
Reference to jQuery.parseJSON: http://api.jquery.com/jQuery.parseJSON/