I've json which looks little complex array of json, I want to parse "1238630400000" and "16.10", like this I need all the values. I'm not getting how we can parse all these values.
This is the code I've tried but no luck:
for (var key in myJSON.Stocks) {
alert(myJSON.Stocks[key].stockPrice);
}
var myJSON = {
"Stocks": {
"stockPrice": [
[1238630400000, 16.10],
[1238716800000, 16.57],
[1238976000000, 16.92],
[1239062400000, 16.43],
[1239148800000, 16.62],
[1239235200000, 17.08],
[1239580800000, 17.17],
[1239667200000, 16.90],
[1239753600000, 16.81],
[1239840000000, 17.35],
[1239926400000, 17.63],
[1241049600000, 17.98]
]
}
}
Can someone help how can i get all these values?
You can get the values by doing a simple forEach on the stockPrice array
myJSON.Stocks.stockPrice.forEach(function(data) { console.log(data[0], data[1]); });
Here is the simplest way:
var csv = myJSON.Stocks.stockPrice.map((o)=>o.join()).join();
console.log(csv);
Related
I have a json like
var obj={
"address":{
"addlin1":"",
"addlin2":""
},
"name":"sam",
"score":[{"maths":"ten",
"science":"two",
"pass":false
}]
}
Now when Iam trying to modify the json iam try an array variable and passing above json to that like
var data=JSON.parse(obj);
var json={};
json['name']=data['name'];
json['address']={};
json['address']['addressline1']=data['address']['addlin1'];
json['address']['addressline2']=data['address']['addlin2'];
json['marks']={};
json['maths']=data['score']['maths'];
For name and address I was able to form the json as i was expecting.But for marks I was unable.May be in obj json score values are in [ ]
So,when i console the json it is in this way
"name":"sam",
"address":{
"addresslin1":"",
"addresslin2":""
},
"score":{}
}
So how can I also read the values inside [] array.
Can someone help me
Thanks
json['maths']=data['score'][0]['maths'];
if you're not sure that data['score'] has any elements you can check prior to reading maths key:
if (data['score'].length) {
json['maths']=data['score'][0]['maths'];
}
data['score'] is an array, so you can't read it like that
json['maths']=data['score']['maths'];
you have to read it like that:
json['maths'] = data['score'][0].maths;
Also, obj is not a JSON, but a JavaScript object. You can use it directly.
json['maths'] = obj['score'][0].maths;
A JSON is a string, like that:
JSON.stringify(obj)
var json = "{"address":{"addlin1":"","addlin2":""},"name":"sam","score":[{"maths":"ten","science":"two","pass":false}]}";
create another json2 to contain score data then assign to json.
for example :
var json={};
json2 = {}
json2[0] = 1;
json2[1] = 2;
json[0] = json2;
I want to extract Lat/Long values from the below mentioned array. Please help me.
var products = {"PolygonCords":"[[51.65040675460229,0.034332275390625],[51.613752957501,0.028839111328125],[51.61034179610213,0.1812744140625],[51.642737480428536,0.157928466796875]]"};
Parse the json string using JSON.parse() and iterate over array using forEach
var products = {
"PolygonCords": "[[51.65040675460229,0.034332275390625],[51.613752957501,0.028839111328125],[51.61034179610213,0.1812744140625],[51.642737480428536,0.157928466796875]]"
};
JSON.parse(products.PolygonCords).forEach(function(v) {
console.log(v[0], v[1])
})
I want to parse the following json:
{"key_410441":{"hashId":"hash123","tube_id":"4accdefk31"}}
Where key_410441 is the entry's name representing the object's value, and the following array is the object's data.
How can I retrieve it's value?
function defined(json) {
for (var i in json) {
var objId = json[i]. ????
}
}
Like Robo Robok said, use Object.keys(object)
if your json look like {"key_410441":{"hashId":"hash123","tube_id":"4accdefk31"}}
function defined(json) {
var hashId = json[Object.keys(json)[0]].hashId
var tube_id = json[Object.keys(json)[0]].tube_id
}
}
you can use shortcut json[Object.keys(json)] because you have olny one object
key_410441
Object keys are returned in form of an array by Object.keys(object)
I suppose you are using jquery and ajax to get a json from an external file. Then the piece of code would be:-
$.getJSON("aa.json", function(data) {
var obj = Object.keys(data),
json = data[obj];
for(var s in json) {
console.log(json[s]);
}
});
I am trying to extract an array from my json string. Please help me. I have the following javascript code,
function lookup(inputString,autoSuggestionsList,TextFieldName)
{
autoSuggestionsListMain=autoSuggestionsList;
inputTextField=TextFieldName;
if(inputString.length == 0)
{
$('#suggestions').hide();
}
else
{
$.post("show_location_hint.php",
{ queryString: ""+inputString+"" },
function(data) { if(data.length >0) { } });
}
}
my show_location_hint.php file will echo the following json string
{"root": {"success":"1","message":"","data":{"locations":[a,b,c]}}}
how can I get the elements from the array locations?
please help me
Thanks
You can parse it to an array using JSON.parse():
var dataArray = JSON.parse(jsonString)
Then just simply do stuff like:
alert(dataArray.root.message)
Try this:
$.post("show_location_hint.php", { queryString: inputString }, function(data) {
if (data) {
var locations = data.root.data.locations;
}
});
Note however that your JSON format is not valid as, a, b, and c should be wrapped in double quotes. I'm guessing this is just a sample of your actual data though.
First your json is well formed. It should be:
{"root": {"success":"1","message":"","data":{"locations":["a","b","c"]}}}
and you can get data like this using jQuery:
var data = {"root": {"success":"1","message":"","data":{"locations":["a","b","c"]}}};
console.log(data.root);
console.log(data.root.success);
console.log(data.root.data);
$.each(data.root.data,function(index,item){
console.log(item);
});
Fiddle DEMO
I wan't to get data from a json file without knowing exacly where the data is:
I have this json
var names= [
{
"category":"category1" ,
"name1":"david",
"name2":"jhon",
"name3":"peter"
},
{
"category":"category2" ,
"name1":"Smith" ,
"name2":"Anna",
}
]
suppose i have a string variable:
var str='category2';
how can i get category2.name1 using the variable?
I don't want to use names[1].name1 because i don't know whats in str and i want to avoid using for loop.
There are some build-in functions that may help you. For example:
var matchingElements = names.filter(function(object, index, array) {
return object.category == str;
});
What you're looking for will always be in matchingElements[0].name1 if matchingElements.length > 0.