I have a JSON file like below:
{
"soils": [{
"mukey": "658854",
"mukeyName": "Meggett-Kenansville-Garcon-Eunola-Blanton-Bigbee (s1517)",
"sl_source": "Fl soil map",
"cokey": "3035468",
"soilName": "Eunola",
"comppct_r": 20,
"compArea": "9.96",
}],
"asfirs": [{
"long": -82.96896600817682,
"lat": 29.977675992923395
}],
"polygon": [{
"rings": [
[
[-9235836.910744485,
3501136.0564117758
],
[-9235798.692230342,
3500237.921329426
],
[-9236553.507884657,
3500667.87961353
],
[-9235836.910744485,
3501136.0564117758
]
]
],
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
}
}]
}
I want extract the value of Polygon key to another JSON object like below
{
"rings": [
[
[-9161396.799823288,
3453315.140590871
],
[-9160708.866568722,
3453095.3841345515
],
[-9161349.02668061,
3452751.4175072685
],
[-9161396.799823288,
3453315.140590871
]
]
],
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
}
}
Now when I do it using
var key3 = 'polygon';
var newPolygonJSON = polygonJson[key3];
var text = JSON.stringify(newPolygonJSON);
where polgonJson contains my initial JSON file I get an extra [] bracket which is not allowing me to create a proper JSON file, like below.
[{
"rings": [
[
[-9235836.910744485,
3501136.0564117758
],
[-9235798.692230342,
3500237.921329426
],
[-9236553.507884657,
3500667.87961353
],
[-9235836.910744485,
3501136.0564117758
]
]
],
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
}
}]
How can I get rid of those [] brackets or extract the value properly?
When you stringify JSON object, it puts extra [] brackets because it takes your object as an array. To extract JSON from text variable, you need to get value of the first (and only) element in that array.
var key3 = 'polygon';
var newPolygonJSON = polygonJson[key3];
var text = JSON.stringify(newPolygonJSON[0]);
Related
I am trying to create postman tests for an API.
The response I am getting from post man is in a json form. It looks like an Object of arrays.
{
"389": [
[
"2021-04-30T00:00:00Z",
16.130089309443093
]
],
"390": [
[
"2021-04-30T00:00:00Z",
14.899161948201808
]
],
"391": [
[
"2021-04-30T00:00:00Z",
17.495245579925736
]
],
"392": [
[
"2021-04-30T00:00:00Z",
16.78176061001777
]
],
"393": [
[
"2021-04-30T00:00:00Z",
25.473437964096448
]
],
"394": [
[
"2021-04-30T00:00:00Z",
56.746358310562826
]
],
"388": [
[
"2021-04-30T00:00:00Z",
18.49559245290604
]
]
}
I am trying to test the integer value that comes after the date is greater than 0 but cant seem to figure out how to traverse the structure in javascript.
With normal response Jsons they usually have the ID beside them and you can use that value, but not with this response
This is the test so far
pm.test("Check performance > 0", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.greaterThan(0);
});
It seems you are testing the whole JSON to be > 0. You should loop throught the values :
pm.test("Check performance > 0", function () {
var jsonData = pm.response.json();
Object.values(jsonData).forEach(record => {
pm.expect(record[0][1]).to.greaterThan(0);
});
/* record will be equal to
[
[
"2021-04-30T00:00:00Z",
16.130089309443093
]
]
and then
[
[
"2021-04-30T00:00:00Z",
14.899161948201808
]
]
and so on */
});
Object.values retrieves the values of a dictionary. See Object.values
(if we want the keys ["389", "390", ...] we can use Object.keys)
I've got an API that returns the following:
{
"data": {
"columns": [
"epoch_timestamp_millieseconds",
"cpu_used_percent"
],
"values": [
[
1615230210000,
28.24
],
...
I'm able to get the second metric using the following three lines of code:
<#assign metricvalue = jsonObj.data.values[0]>
<#assign arr = metricvalue[1]>
&value=${arr}
&value would equal 28.24.
Is there a way to combine these into one line of code?
I'm looking for something like this:
&value=jsonObj.data.values[0].[1]
The issue is the [1] doesn't have a label
Your code is almost correct. Just a small correction. Remove the . between [0] and [1]. You don't need to use . to specify the index. You only need to use it to specify the property/key.
The code should be like
jsonObj.data.values[0][0] // 1615230210000
jsonObj.data.values[0][1] // 28.24
Sure! You were very close. Just remove the dot:
const response = {
"data": {
"columns": [
"epoch_timestamp_millieseconds",
"cpu_used_percent"
],
"values": [
[
1615230210000,
28.24
]
]
}
};
console.log(response.data.values[0][1]);
You can use destructuring as well:
const response = {
"data": {
"columns": [
"epoch_timestamp_millieseconds",
"cpu_used_percent"
],
"values": [
[
1615230210000,
28.24
]
]
}
};
const { data: { values: [[_, target]] } } = response;
console.log(target)
I'm pretty lost right now and have been working on this for about 6 days now so forgive me if this is a bit confusing. I'm using NVD3 to display some graphs based on data that comes in from BigQuery. All the data coming in is correct and so is the graph implementation, the issue is the actual JSON data. The multi bar chart requires that each "set" of data have the same dates and same number of values under the initial array. Based off my data, sometimes there will be missing values if a user didn't log an event or something that day.
The general idea of what I'm trying to do here is loop through the initial json and append on the "missing" values. For example this would be the initial data that I get out of BigQuery and my API:
[
"t1":{
"target": "t1",
"datapoints": [
[
16.0,
1483747200.0
],
[
10.0,
1484352000.0
]
]
},
"t2":{
"target": "t2",
"datapoints": [
[
10.0,
1483660800.0
],
[
19.0,
1484006400.0
],
[
10.0,
1484956800.0
]
]
}
]
You can see here that the first object has a datapoints array with 2 values, object two has a datapoints array with 3 values. The 1 index of the datapoints array contains a UNIX date, every datapoints array within the entire object must have an array with the date and then 0 for a default value. So the formatted data would look something like this:
[
"t1":{
"target": "t1",
"datapoints": [
[
16.0,
1483747200.0
],
[
10.0,
1484352000.0
],
[
0.0,
1483660800.0
],
[
0.0,
1484006400.0
],
[
0.0,
1484956800.0
]
]
},
"t2":{
"target": "t2",
"datapoints": [
[
10.0,
1483660800.0
],
[
19.0,
1484006400.0
],
[
10.0,
1484956800.0
],
[
0.0,
1483747200.0
],
[
0.0,
1484352000.0
]
]
}
]
I really have no idea where to go from here and any help whatsoever would be extremely helpful. I've been working on this for days and at this point am just grinding my gears. Thanks
Basically, each value that's found in one array but not in others should take the timestamp but set the first value/index to 0.
I should also mention that the query is only querying for 30 days back so at most each one of the datapoints arrays would have 30 arrays.
You will have to do a bit of data processing first to get all of the dates, then it's just a matter of filling in the actual data for each date.
const json = [
{
"target": "t1",
"datapoints": [
[
16.0,
1483747200.0
],
[
10.0,
1484352000.0
]
]
},
{
"target": "t2",
"datapoints": [
[
10.0,
1483660800.0
],
[
19.0,
1484006400.0
],
[
10.0,
1484956800.0
]
]
}
]
// using es6 set
const dates = new Set()
json.forEach( x => x.datapoints.map( dp => dates.add(dp[1]) ) )
// all dates are there
dates.forEach( d => console.log(d) )
const fillDp = dp => {
return Array.from(dates).sort().map( d => dp.find( x => x[1] === d ) || [0,d] )
}
const result = json.map( x => Object.assign(x, {datapoints: fillDp(x.datapoints)}) )
console.log(JSON.stringify(result[0].datapoints))
Below is a quick and dirty solution. Note that I added an additional entry to the sample you provided so that both data objects contain a data point with a common date.
var orig= [
{
"target": "t1",
"datapoints": [
[
16.0,
1483747200.0
],
[
10.0,
1484352000.0
],
[
10.0,
1483660800.0
]
]
},
{
"target": "t2",
"datapoints": [
[
10.0,
1483660800.0
],
[
19.0,
1484006400.0
],
[
10.0,
1484956800.0
]
]
}
];
console.log('Original Data', JSON.stringify(orig));
// Get a list of all the datapoint dates
var dates = [];
orig.forEach(function(item) {
item.datapoints.forEach(function(dp) {
var date = dp[1];
dates.push(date);
})
});
console.log('All Dates', JSON.stringify(dates));
// Remove duplicates from array
dates = dates.filter(function (el, i, arr) {
return arr.indexOf(el) === i;
});
console.log('Unique Dates', JSON.stringify(dates));
// Check each item in the original array for records for each date and add a
// 0 value entry if the date entry is missing
dates.forEach(function(dt) {
orig.forEach(function(item, itemIndex) {
var hasEntry = false;
item.datapoints.forEach(function(dp) {
if (dp[1] === dt) hasEntry = true;
});
if (!hasEntry) {
item.datapoints.push([0, dt]);
}
});
});
console.log('Updated Data', JSON.stringify(orig));
And here is the corresponding plunker so you can see it in action: https://plnkr.co/edit/K1NK2Xx8RNrqyp7yZ3n2?p=preview
I am very new to JSON, stuck in parsing multi level JSON array, I want to parse it using javascript or jquery. From the JSON I want to get application id, application description & Product description
[
{
"roadMapData": [
{
"applicationDetail": [
{
"applicationDescr": "R25updated-R25updated",
"applicationId": 352
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 271,
"productSubGroupDes": "TEST123-TEST1234"
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 278,
"productSubGroupDes": "ggg-hhhh"
}
]
}
]
},
{
"roadMapData": [
{
"applicationDetail": [
{
"applicationDescr": "R25updated-R25updated",
"applicationId": 352
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 271,
"productSubGroupDes": "TEST123-TEST1234"
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 278,
"productSubGroupDes": "ggg-hhhh1"
}
]
}
]
}
]
Thanks in advance :)
Here is the Demo
Check jQuery.parseJSON
var jsonObj = jQuery.parseJSON(jsonString);
for (i = 0; i < jsonObj.length; i++) {
var roadMapData = jsonObj[i].roadMapData;
var applicationDetail = roadMapData[0].applicationDetail; //First Object
var productSubGrupDetail1 = roadMapData[1].productSubGrupDetail; //Second Object
var productSubGrupDetail2 = roadMapData[2].productSubGrupDetail; //Third Object
console.log(applicationDetail[0].applicationDescr); //applicationDetail's First Object
console.log(productSubGrupDetail1[0].productGroupId); //productSubGrupDetail1's First Object
console.log(productSubGrupDetail2[0].productSubGroupDes); //productSubGrupDetail2's First Object
}
If data initially presented in JSON (as a string), you need first parse it into JavaScript object with JSON.parse(json). Then you can access any property with object dot notation. If you are not familiar with objects in JavaScript, check this article.
I have a JSON like this:
{
"default": [
[
1325876000000,
0
],
[
1325876000000,
0
],
[
1325876000000,
0
],
[
1325876000000,
0
]
],
"direct": [
[
1328196800000,
0
],
[
1328196800000,
100
],
[
1328196800000,
0
],
[
1328196800000,
0
]
],
"Sales": [
[
1330517600000,
0
],
[
1330517600000,
0
],
[
1330517600000,
90
],
[
1330517600000,
0
]
],
"Support": [
[
1332838400000,
0
],
[
1332838400000,
0
],
[
1332838400000,
0
],
[
1332838400000,
0
]
]
}
I want to generate array contains the name of each item and the first value of the corresponing array. the result should be like this:
ticks = [["default", 1325876000000],["direct", 1328196800000],["Sales", 1330517600000],["Support", 1332838400000]]
the names like default, direct, sales, supportare dynamic so I can't do jsondata.support
what I tried
ticks = []
for key in jsondata{
arraynew = [];
arraynew.push(key)
}
but I don't know how to push the values?
Help please.
You just need to access the sub-array.
var ticks = [];
for (var key in jsondata) {
ticks.push( [ key, jsondata[key][0][0] ] );
}
The expression jsondata[key] gets you the outer array corresponding to each key. Then, jsondata[key][0] gets you the first of the sub-arrays, and adding the final [0] to that gets you the first value in the first sub-array.
Note that you're not guaranteed to get the keys back in any particular order.