I am trying to parse the following JSON payload:
{
"results":[
[
298.648132,
280.68692,
356.54184,
388.085541,
183.491806,
-484.676086,
-468.069916,
-446.741699
],
[
299.641846,
285.005798,
358.563812,
389.283997,
212.144806,
-485.533844,
-469.071533,
-447.885406
],
[
302.24469,
291.76059,
362.658936,
392.376129,
217.732513,
-484.816711,
-468.566711,
-447.615082
],
[
303.058899,
297.929199,
365.46994,
393.894928,
213.591797,
-486.055756,
-469.872986,
-449.343323
],
[
304.604095,
304.826233,
369.112122,
396.274597,
206.882492,
-486.385498,
-470.249512,
-450.089935
],
[
305.541901,
306.31842,
370.016907,
396.985413,
200.299408,
-486.345032,
-470.176208,
-450.01059
],
[
305.137024,
306.015381,
369.381042,
396.26059,
196.422821,
-487.490143,
-471.321533,
-451.191711
],
[
306.182373,
307.574707,
370.42627,
397.127747,
206.874603,
-487.188477,
-471.038483,
-450.869781
],
[
307.108887,
309.183777,
371.413666,
397.890198,
234.509079,
-486.957367,
-470.937103,
-450.646393
],
[
308.208923,
310.277588,
372.322968,
398.777618,
244.5168,
-486.2995,
-470.352631,
-449.89325
],
[
308.676208,
310.526123,
372.360626,
398.743317,
250.976288,
-486.666687,
-470.868408,
-450.324463
],
[
308.910583,
310.629242,
372.255676,
398.59491,
252.538498,
-487.001068,
-471.305817,
-450.616699
]
],
"columns":[
"5bb6a5d20ff4c313aab7241c.value.value",
"5bb6a5d30ff4c313aab72421.value.value",
"5bb6a5d30ff4c313aab72425.value.value",
"5bb6a5d30ff4c313aab72426.value.value",
"5bb6a5d30ff4c313091fe079.value.value",
"5bb6a5d30ff4c313091fe07b.value.value",
"5bb6a5d30ff4c313091fe07c.value.value",
"5bb6a5d40ff4c313091fe07f.value.value"
]
}
I am only interested in the "results" section. I will end up plotting these values.
I am proficient in C++, but new to JavaScript.
I've found plenty of examples with typical JSON strings, but I haven't found anything that works for this yet.
Thanks for the help.
Try the JSON.parse() function:
const payload = // your json payload
const json = JSON.parse(payload);
const results = json.results;
If you can copy-paste your json to your JS code directly then try
const payload = {"results": [ ... } // your json
const results = payload.results;
const payload ={"results":[[298.648132,280.68692,356.54184,388.085541,183.491806,-484.676086,-468.069916,-446.741699],
[299.641846,285.005798,358.563812,389.283997,212.144806,-485.533844,-469.071533,-447.885406],
[302.24469,291.76059,362.658936,392.376129,217.732513,-484.816711,-468.566711,-447.615082],
[303.058899,297.929199,365.46994,393.894928,213.591797,-486.055756,-469.872986,-449.343323],
[304.604095,304.826233,369.112122,396.274597,206.882492,-486.385498,-470.249512,-450.089935],
[305.541901,306.31842,370.016907,396.985413,200.299408,-486.345032,-470.176208,-450.01059],
[305.137024,306.015381,369.381042,396.26059,196.422821,-487.490143,-471.321533,-451.191711],
[306.182373,307.574707,370.42627,397.127747,206.874603,-487.188477,-471.038483,-450.869781],
[307.108887,309.183777,371.413666,397.890198,234.509079,-486.957367,-470.937103,-450.646393],
[308.208923,310.277588,372.322968,398.777618,244.5168,-486.2995,-470.352631,-449.89325],
[308.676208,310.526123,372.360626,398.743317,250.976288,-486.666687,-470.868408,-450.324463],
[308.910583,310.629242,372.255676,398.59491,252.538498,-487.001068,-471.305817,-450.616699]],
"columns":["5bb6a5d20ff4c313aab7241c.value.value","5bb6a5d30ff4c313aab72421.value.value",
"5bb6a5d30ff4c313aab72425.value.value","5bb6a5d30ff4c313aab72426.value.value",
"5bb6a5d30ff4c313091fe079.value.value","5bb6a5d30ff4c313091fe07b.value.value",
"5bb6a5d30ff4c313091fe07c.value.value","5bb6a5d40ff4c313091fe07f.value.value"]}
const results = payload.results;
console.log(results);
I have an array with multiple elements that looks like this:
{
"name": "DR",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
]
],
"type": "areaspline"
},
{
"name": "SIT",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
],
[
"1506384000000",
3
],
[
"1534204800000",
1
],
[
"1531094400000",
1
],
[
"1528502400000",
1
]
],
"type": "areaspline"
},
This is the exact format I use to send data into high charts, however, the problem is that the chart breaks if the timestamps inside each environment (DR, SIT) are not in order.
How can I sort the 'data' inside each environment by timestamp?
This JSON is generated in PHP and sent through to JavaScript. So I would like to know how to sort the data inside either PHP or JavaScript.
Thanks.
This is actually rather trivial, once you know about the usort function. It allows you to define your own sorting function, so you can sort based on any factor of whatever the 2 objects it is passed.
Note that from your example json I had to add a set of square brackets around the whole thing to get PHP to parse it with json_decode .
<?php
// note I had to add square brackets to your
// demo json ....
$json='[{
"name": "DR",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
]
],
"type": "areaspline"
},
{
"name": "SIT",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
],
[
"1506384000000",
3
],
[
"1534204800000",
1
],
[
"1531094400000",
1
],
[
"1528502400000",
1
]
],
"type": "areaspline"
}]';
$json_obj_arr=json_decode($json,true);
print_r($json_obj_arr);
print("\n\n");
// a function to handle the sorting itself
// usort will pass it an array(timestamp,datavalue) for both
// $a and $b so we compare the [0] element of each
function cmp($a, $b)
{
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
}
// loop through the array, first is indexed
for($i=0;$i<count($json_obj_arr);$i++){
// then each one of those contains an
// associtive array with an element named ['data'] -
// this is an indexed array that you want sorted
// on the [0] element
usort($json_obj_arr[$i]['data'],"cmp");
}
print_r($json_obj_arr);
print("\n\n");
?>
I am formatting some data to be more readable.
I am sending this data to the frontend, look
code
res.status(200).json({dealersData});
dealersData is a json containing
{
"Dealers":{
"Detail":[
{
"Table":[
{
"DealerId":[
"1"
],
"DealerName":[
"Carmen"
],
"NickName":[
"Carmen"
],
"Picture":[
"00001.jpg"
],
"Active":[
"1"
],
"LegalId":[
"111111111"
],
"TypeId":[
"1"
]
}
]
}
]
}
}
so what I need to do with Lodash is send the data to the frontend without the '"Dealers":{...}and without the '"Detail":[...]' part, I need to send it starting the JSON from"Table"`
lets say like this
{
"Table":[
{
"DealerId":[
"1"
],
"DealerName":[
"Carmen"
],
"NickName":[
"Carmen"
],
"Picture":[
"00001.jpg"
],
"Active":[
"1"
],
"LegalId":[
"111111111"
],
"TypeId":[
"1"
]
}
I already tried but I am getting something like
dealersData {[[null]]}
so, what are your suggestions ?
res.status(200).json(dealersData.Dealers.Detail[0]);
Access Dealers key, then first object in Detail array
Just change your response to:
res.status(200).json(dealersData.Dealers.Detail[0]);
Perhaps check data validity:
if(dealersData && dealersData.Dealers && dealersData.Dealers.Detail) {
res.status(200).json(dealersData.Dealers.Detail[0]);
} else {
res.status(404).json({'success':false});
}
I like to know how to construct or store json data for City, State and Country in a single variable ?
I have seen one but it only deal with single kind of data means CAR
{
"cars": {
"Nissan": [
{"model":"Sentra", "doors":4},
{"model":"Maxima", "doors":4}
],
"Ford": [
{"model":"Taurus", "doors":4},
{"model":"Escort", "doors":4}
]
}
}
My intention is to store different kind of json data in single area or in single variable.
What about this ?
{
"countries": [
{...}
],
"states" [
{...}
],
"cities": [
{...}
]
}
maybe this is a direct answer.
{
"Countries":[{
"CountryName":"Indonesia",
"States":[{
"StateName":"Bali",
"Cities":["Denpasar",
"Kuta",
"Tuban"
]},
{
"StateName":"Jakarta",
"Cities":[
"Bandung",
"Tanggerang"
]
}
]
}]}
check the file here
jsonforcountriesstatecities
Hi My Json Looks like below and i want to parse it to bind the data to array. Please provide me how i can parse it?
[
{
"ImgUrl": "http://www.cowboysalamocityharley.com/used_bikes/DSC_7815.JPG",
"CarouselImages": [
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7816.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7817.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7818.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7819.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7820.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7821.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7822.JPG"
],
"VehicleId": 1
},
{
"ImgUrl": "http://www.cowboysalamocityharley.com/used_bikes/DSC_7816.JPG",
"CarouselImages": [
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7816.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7817.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7818.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7819.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7820.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7821.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7822.JPG"
],
"VehicleId": 2
},
{
"ImgUrl": "http://www.cowboysalamocityharley.com/used_bikes/DSC_7817.JPG",
"CarouselImages": [
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7816.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7817.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7818.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7819.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7820.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7821.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7822.JPG"
],
"VehicleId": 3
},
{
"ImgUrl": "http://www.cowboysalamocityharley.com/used_bikes/DSC_7818.JPG",
"CarouselImages": [
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7816.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7817.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7818.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7819.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7820.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7821.JPG",
"http://www.cowboysalamocityharley.com/used_bikes/th_DSC_7822.JPG"
],
"VehicleId": 4
}
]
is there any way i can parse it using an inbuilt library or shall i need to loop through?
try this
var json = eval("(" + result + ")");
Try this:
var arr = JSON.parse(data);