Related
I have a JSON array which includes a nested array:
{
"data": [
{
"id": 659,
"created_at": "2023-01-13T06:35:08.000000Z",
"products": [
{
"name": "532",
"properties": [
{
"name": "color",
"value": "blue"
},
{
"name": "size",
"value": "1"
}
],
}
]
},
{
"id": 658,
"created_at": "2023-01-12T21:36:06.000000Z",
"products": [
{
"name": "532",
"properties": [
{
"name": "color",
"value": "khaki"
},
{
"name": "size",
"value": "2"
}
],
}
]
},
{
"id": 656,
"created_at": "2023-01-11T17:19:42.000000Z",
"products": [
{
"name": "2245/442",
"properties": [
{
"name": "color",
"value": "gray"
},
{
"name": "fabric",
"value": "fleece"
},
{
"name": "size",
"value": "2"
}
],
}
]
},
{
"id": 655,
"created_at": "2023-01-10T21:56:51.000000Z",
"products": [
{
"name": "298/426-2",
"properties": [
{
"name": "color",
"value": "blue"
},
{
"name": "fabric",
"value": "footer"
},
{
"name": "size",
"value": "4"
}
],
},
{
"name": "257/426",
"properties": [
{
"name": "color",
"value": "mint"
},
{
"name": "fabric",
"value": "footer"
},
{
"name": "size",
"value": "4"
}
],
}
]
},
],
}
I need to read it and write it to another array. All code I have so far:
var responce = UrlFetchApp.fetch(url, options);
var dataAll = JSON.parse(responce.getContentText());
var dataSet = dataAll.data;
var rows = [];
dataSet.forEach((e) => {
e.products.forEach((product) => {
product.properties.forEach((property) => {
rows.push([e.id, e.created_at, product.name, property.value]);
})
});
});
console.log(rows);
It writes data to an array in the form, that is, for each "product.value" value, it writes "id", "created_at", "name":
[ 659, '2023-01-13T06:35:08.000000Z', '532', 'blue' ],
[ 659, '2023-01-13T06:35:08.000000Z', '532', '1' ],
[ 658, '2023-01-12T21:36:06.000000Z', '532', 'khaki' ],
[ 658, '2023-01-12T21:36:06.000000Z', '532', '2' ],
[ 656, '2023-01-11T17:19:42.000000Z', '2245/442', 'gray' ],
[ 656, '2023-01-11T17:19:42.000000Z', '2245/442', 'fleece' ],
[ 656, '2023-01-11T17:19:42.000000Z', '2245/442', '2' ],
[ 655, '2023-01-10T21:56:51.000000Z', '298/426-2', 'blue' ],
[ 655, '2023-01-10T21:56:51.000000Z', '298/426-2', 'footer' ],
[ 655, '2023-01-10T21:56:51.000000Z', '298/426-2', '4' ],
[ 655, '2023-01-10T21:56:51.000000Z', '257/426', 'mint' ],
[ 655, '2023-01-10T21:56:51.000000Z', '257/426', 'footer' ],
[ 655, '2023-01-10T21:56:51.000000Z', '257/426', '4' ]
But I need to write like:
[ 659, '2023-01-13T06:35:08.000000Z', '532', 'blue', '1' ],
[ 658, '2023-01-12T21:36:06.000000Z', '532', 'khaki', '2' ],
[ 656, '2023-01-11T17:19:42.000000Z', '2245/442', 'gray', 'fleece', '2' ],
[ 655, '2023-01-10T21:56:51.000000Z', '298/426-2', 'blue', 'footer', '4' ],
[ 655, '2023-01-10T21:56:51.000000Z', '257/426', 'mint', 'footer', '4' ]
That is, I need to read the array "properties" and write the values "values" to a string (maybe I'm not expressing myself correctly).
I understand that I need to use the "map" function, but I don't understand how to use it yet ((
Thanks in advance for any advice
dataSet.forEach((e) => {
e.products.forEach((product) => {
rows.push([
e.id, e.created_at, product.name
].concat(product.properties.map(p => p.value)));
});
});
const rows = dataSet.map((item) => {
const newRows = [item.id, item.created_at];
item.products.forEach((product) => {
const properties = product.properties.map((property) => property.value);
newRows.push(product.name);
newRows.push(...properties);
});
return newRows;
});
console.log(rows);
I have the following array
[ {
"contactId": "a87d096gd5fuop",
"firstName": "John Doe",
"registrationTypes": {
"selectedOptions": [
{
}
],
"subTotal": 1620.003
},
"foo1": {
"selectedOptions": [
],
"subTotal": 0
},
"events": {
"selectedOptions": [
{
"id": "1",
"name": "T1",
"value": "4550006:3",
},
{
"id": "2",
"name": "T2",
"value": "4550005:3",
},
{
"id": "3",
"name": "T3",
"value": "4550003:3",
}
],
"subTotal": 135.003
},
"freeNetworkingFunctions": {
},
"total": 1755.0059999999999
},
{
"contactId": "a097f",
"firstName": "David",
"registrationTypes": {
"selectedOptions": [
{}
],
"subTotal": 899.998
},
"foo1": {
"selectedOptions": [
],
"subTotal": 0
},
"member": {
"selectedOptions": [
{
}
],
"subTotal": 228.8
},
"events": {
"selectedOptions": [
{
"id": "4",
"name": "T4",
"value": "4550002:2",
},
{
"id": "5",
"name": "T5",
"value": "4550001:2",
},
{
"id": "6",
"name": "T6",
"value": "4550003:2",
}
],
"subTotal": 135.003
},
"total": 1263.801
}
]
From the above array, I want to extract events, loop all the data and get only values. So my new array should be something like this:
[ {
"contactId": "a87d096gd5fuop",
"firstName": "John Doe",
"registrationTypes": {
"selectedOptions": [
{
}
],
"subTotal": 1620.003
},
"foo1": {
"selectedOptions": [
],
"subTotal": 0
},
"events": [
"4550006:3"
"4550005:3",
"4550003:3",
],
},
"freeNetworkingFunctions": {
},
"total": 1755.0059999999999
},
{
"contactId": "a097f",
"firstName": "David",
"registrationTypes": {
"selectedOptions": [
{}
],
"subTotal": 899.998
},
"foo1": {
"selectedOptions": [
],
"subTotal": 0
},
"member": {
"selectedOptions": [
{
}
],
"subTotal": 228.8
},
"events": [
"4550004:2"
"4550008:3",
"4550003:3",
],
"subTotal": 135.003
},
"total": 1263.801
}
]
So it should return the original array, however, events value data should be in one array.
var arr = [];
var r(var i=0;i<data.length;i++){
data.push(arr[i].value);
}
var newData = [...data, arr]
However, this doesn't work. Any help would be highly appreciated.
Use map twice - once on the dataset to iterate over the objects, and within that map to get an array of values from the selectedOptions.
const data=[{contactId:"a87d096gd5fuop",firstName:"John Doe",registrationTypes:{selectedOptions:[{}],subTotal:1620.003},foo1:{selectedOptions:[],subTotal:0},events:{selectedOptions:[{id:"1",name:"T1",value:"4550006:3"},{id:"2",name:"T2",value:"4550005:3"},{id:"3",name:"T3",value:"4550003:3"}],subTotal:135.003},freeNetworkingFunctions:{},total:1755.0059999999999},{contactId:"a097f",firstName:"David",registrationTypes:{selectedOptions:[{}],subTotal:899.998},foo1:{selectedOptions:[],subTotal:0},member:{selectedOptions:[{}],subTotal:228.8},events:{selectedOptions:[{id:"4",name:"T4",value:"4550002:2"},{id:"5",name:"T5",value:"4550001:2"},{id:"6",name:"T6",value:"4550003:2"}],subTotal:135.003},total:1263.801}];
const out = data.map(obj => {
// Destructure the selected options from the
// rest of each object
const { events: { selectedOptions }, ...rest } = obj;
// `map` over the options to just get an array of values
const events = selectedOptions.map(option => {
return option.value;
});
// Return a new object with the new events property
// combined with the other properties again
return { ...rest, events };
});
console.log(out);
Additional documentation
Destructuring assignment
Rest parameters
Spread syntax
I have a javascript JSON variable and I'm trying to feed it to the JSTree. It is not rendering anything.
{
"id":1,
"data":"GDC",
"depth":-1,
"children":[
{
"id":2,
"data":"SICS",
"depth":0,
"children":[
{
"id":5,
"data":"Collaboration",
"depth":1,
"children":[
{
"id":10,
"data":"Contact Center",
"depth":2,
"children":[
{
"id":607,
"data":"Subscription",
"depth":3,
"children":[
{
"depth":4,
"id":608,
"children":[
],
"data":"Optimization"
}
]
},
{
"id":606,
"data":"Transaction",
"depth":3,
"children":[
{
"depth":4,
"id":664,
"children":[
],
"data":"Planning"
},
{
"depth":4,
"id":672,
"children":[],
}
]
}
]
},
{
"id":11,
"data":"Productivity",
"depth":2,
"children":[
{
"id":1231,
"data":"DevOps",
"depth":3,
"children":[
{
"depth":4,
"id":1280,
"children":[
],
"data":"Deployment"
}
]
},
{
"id":1229,
"data":"Tool Support",
"depth":3,
"children":[
{
"depth":4,
"id":1232,
"children":[
],
"data":"UCDT"
},
{
"depth":4,
"id":1237,
"children":[
],
"data":"PCAT"
}
]
}
]
}
]
},
{
"id":3,
"data":"Security ",
"depth":1,
"children":[
{
"id":284,
"data":"Security",
"depth":2,
"children":[
{
"id":1286,
"data":"Subscription",
"depth":3,
"children":[
{
"depth":4,
"id":1491,
"children":[
],
"data":"Software Strategy"
},
{
"depth":4,
"id":1496,
"children":[
],
"data":"Change Support/Migration"
}
]
},
{
"id":1285,
"data":"Transaction",
"depth":3,
"children":[
{
"depth":4,
"id":1287,
"children":[
],
"data":"CRD"
},
{
"depth":4,
"id":1290,
"children":[
],
"data":"NIP"
}
]
}
]
}
]
}
]
}
]
}
Ive feeded this data into JSTree but its not rendering anything.
I've tried using a basic JSON data, but it takes that. When I have nested children it seems to fail.
th JS code is :
$(function () {
$('#jstree').jstree({
'json_data' : {
'data' : data
}
});
});
Any ideas?
Below is your updated json(change your "data" to "text" property):
var data = {
"id": 1,
"text": "GDC",
"depth": -1,
"children": [
{
"id": 2,
"text": "SICS",
"depth": 0,
"children": [
{
"id": 5,
"text": "Collaboration",
"depth": 1,
"children": [
{
"id": 10,
"text": "Contact Center",
"depth": 2,
"children": [
{
"id": 607,
"text": "Subscription",
"depth": 3,
"children": [
{
"depth": 4,
"id": 608,
"children": [
],
"text": "Optimization"
}
]
},
{
"id": 606,
"text": "Transaction",
"depth": 3,
"children": [
{
"depth": 4,
"id": 664,
"children": [
],
"text": "Planning"
},
{
"depth": 4,
"id": 672,
"children": [],
}
]
}
]
},
{
"id": 11,
"text": "Productivity",
"depth": 2,
"children": [
{
"id": 1231,
"text": "DevOps",
"depth": 3,
"children": [
{
"depth": 4,
"id": 1280,
"children": [
],
"text": "Deployment"
}
]
},
{
"id": 1229,
"text": "Tool Support",
"depth": 3,
"children": [
{
"depth": 4,
"id": 1232,
"children": [
],
"text": "UCDT"
},
{
"depth": 4,
"id": 1237,
"children": [
],
"text": "PCAT"
}
]
}
]
}
]
},
{
"id": 3,
"text": "Security ",
"depth": 1,
"children": [
{
"id": 284,
"text": "Security",
"depth": 2,
"children": [
{
"id": 1286,
"text": "Subscription",
"depth": 3,
"children": [
{
"depth": 4,
"id": 1491,
"children": [
],
"text": "Software Strategy"
},
{
"depth": 4,
"id": 1496,
"children": [
],
"text": "Change Support/Migration"
}
]
},
{
"id": 1285,
"text": "Transaction",
"depth": 3,
"children": [
{
"depth": 4,
"id": 1287,
"children": [
],
"text": "CRD"
},
{
"depth": 4,
"id": 1290,
"children": [
],
"text": "NIP"
}
]
}
]
}
]
}
]
}
]
}
And below is correct js code:
$('#jstree').jstree({
'core': {
'data': data
}
});
I'm using Google analytics API to get a JSON message from the server. The message I receive is this one :
{
"reports": [
{
"columnHeader": {
"dimensions": [
"ga:landingPagePath"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:pageviews",
"type": "INTEGER"
},
{
"name": "ga:sessions",
"type": "INTEGER"
}
]
}
},
"data": {
"rows": [
{
"dimensions": [
"/-chandigarh/axis-bank-sarsini-branch_chandigarh_chg_850458.html"
],
"metrics": [
{
"values": [
"1",
"1"
]
}
]
},
{
"dimensions": [
"/267249-1.compliance-alex.xyz"
],
"metrics": [
{
"values": [
"29",
"10"
]
}
]
},
{
"dimensions": [
"/267249-1.compliance-don.xyz"
],
"metrics": [
{
"values": [
"27",
"9"
]
}
]
},
{
"dimensions": [
"/267249-1.compliance-fred.xyz"
],
"metrics": [
{
"values": [
"20",
"7"
]
}
]
},
{
"dimensions": [
"/abohar/axis-bank-the-fazilka-central-cooperative-bank-ltd-branch_abohar_frp_135.html"
],
"metrics": [
{
"values": [
"1",
"1"
]
}
]
},
{
"dimensions": [
"/about-us/career.htm"
],
"metrics": [
{
"values": [
"8",
"5"
]
}
]
},
{
"dimensions": [
"/about-us/company-profile.htm"
],
"metrics": [
{
"values": [
"34",
"14"
]
}
]
},
{
"dimensions": [
"/about-us/infrastructure.htm"
],
"metrics": [
{
"values": [
"3",
"1"
]
}
]
},
{
"dimensions": [
"/adilabad/gk-hospital-multispeciality-care_adilabad_adi_399806.html"
],
"metrics": [
{
"values": [
"2",
"1"
]
}
]
},
{
"dimensions": [
"/ahmedabad/akhani-jagdish-kumar_ahmedabad_ahd_1124498.html"
],
"metrics": [
{
"values": [
"7",
"3"
]
}
]
}
],
"totals": [
{
"values": [
"3420452",
"1333496"
]
}
],
"rowCount": 347614,
"minimums": [
{
"values": [
"0",
"1"
]
}
],
"maximums": [
{
"values": [
"56660",
"49274"
]
}
],
"isDataGolden": true
},
"nextPageToken": "1000"
}
]
}
I want to parse it and saved data in variable. How will I parse it. I tried many options but didn't get any data from JSON. Result is showing like undefined. I want to fetch the array data of dimensions and values like:
var a = "/-chandigarh/axis-bank-sarsini-branch_chandigarh_chg_850458.html";
var b = 1;
var c = 1;
Supposing your JSON input is stored in the json variable, you could just do:
var json = '{"reports":[{"columnHeader":{"dimensions":["ga:landingPagePath"],"metricHeader":{"metricHeaderEntries":[{"name":"ga:pageviews","type":"INTEGER"},{"name":"ga:sessions","type":"INTEGER"}]}},"data":{"rows":[{"dimensions":["/-chandigarh/axis-bank-sarsini-branch_chandigarh_chg_850458.html"],"metrics":[{"values":["1","1"]}]},{"dimensions":["/267249-1.compliance-alex.xyz"],"metrics":[{"values":["29","10"]}]},{"dimensions":["/267249-1.compliance-don.xyz"],"metrics":[{"values":["27","9"]}]},{"dimensions":["/267249-1.compliance-fred.xyz"],"metrics":[{"values":["20","7"]}]},{"dimensions":["/abohar/axis-bank-the-fazilka-central-cooperative-bank-ltd-branch_abohar_frp_135.html"],"metrics":[{"values":["1","1"]}]},{"dimensions":["/about-us/career.htm"],"metrics":[{"values":["8","5"]}]},{"dimensions":["/about-us/company-profile.htm"],"metrics":[{"values":["34","14"]}]},{"dimensions":["/about-us/infrastructure.htm"],"metrics":[{"values":["3","1"]}]},{"dimensions":["/adilabad/gk-hospital-multispeciality-care_adilabad_adi_399806.html"],"metrics":[{"values":["2","1"]}]},{"dimensions":["/ahmedabad/akhani-jagdish-kumar_ahmedabad_ahd_1124498.html"],"metrics":[{"values":["7","3"]}]}],"totals":[{"values":["3420452","1333496"]}],"rowCount":347614,"minimums":[{"values":["0","1"]}],"maximums":[{"values":["56660","49274"]}],"isDataGolden":true},"nextPageToken":"1000"}]}'
// Parse the JSON into the data variable
var data = JSON.parse(json);
data.reports.forEach(report => {
report.data.rows.forEach(row => {
// row.dimensions will contain your 'dimensions' array
console.log(row.dimensions);
row.metrics.forEach(metric => {
// metric.values will contain your 'values' array
console.log(metric.values);
});
});
});
You will just have to store these properties into your own variables.
Use json.parse
var json = '{"param1":1,"param2":2}',
obj = JSON.parse(json);
alert(obj.param1);
Most browsers e.g. Google support JSON.parse, moreover for those which don't support you can use json2 -> https://github.com/douglascrockford/JSON-js/blob/master/json2.js
I am getting an array of specs from flipkart's API.
I am able to get this response from that API:
{
"nextUrl": "https://affiliate-api.flipkart.net/1.0/affiliate/feeds/fnkygygma/category/reh/55ab6c2673a4773ffb8e4019.json?expiresAt=1452881213871&sig=1c4c5111b6b014a71a17b229e6df6afc",
"validTill": 1452881213871,
"productInfoList": [
{
"productBaseInfoV1": {
"productId": "TDHDMH5GRSPZ3DNM",
"title": "Newhide Designer",
"productDescription": "",
"imageUrls": {
"400x400": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-400x400-imadmh8zgxbrsgq6.jpeg",
"200x200": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-200x200-imadmh8zgxbrsgq6.jpeg",
"unknown": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-original-imadmh8zgxbrsgq6.jpeg",
"800x800": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-800x800-imadmh8zgxbrsgq6.jpeg"
},
"productFamily": null,
"maximumRetailPrice": {
"amount": 1145,
"currency": "INR"
},
"flipkartSellingPrice": {
"amount": 1145,
"currency": "INR"
},
"flipkartSpecialPrice": {
"amount": 1145,
"currency": "INR"
},
"productUrl": "http://dl.flipkart.com/dl/newhide-designer/p/itmdp2nunbzffwzr?pid=TDHDMH5GRSPZ3DNM&affid=keshav",
"productBrand": "Newhide",
"inStock": true,
"codAvailable": true,
"discountPercentage": 0,
"offers": [],
"categoryPath": "[[{\"node_id\":20001,\"node_name\":\"FLIPKART_TREE\"},{\"node_id\":21183,\"node_name\":\"Lifestyle\"},{\"node_id\":21499,\"node_name\":\"Leather \\u0026 Travel Accessories\"},{\"node_id\":21960,\"node_name\":\"Wallets \\u0026 Clutches\"},{\"node_id\":21274,\"node_name\":\"Wallets \\u0026 Card Wallets\"}]]",
"styleCode": null,
"attributes": {
"size": "",
"color": "Black",
"storage": "",
"sizeUnit": "",
"displaySize": ""
}
},
"productShippingInfoV1": {
"shippingCharges": {
"amount": 0,
"currency": "INR"
},
"sellerName": null,
"sellerAverageRating": null,
"sellerNoOfRatings": 0,
"sellerNoOfReviews": 0
},
"categorySpecificInfoV1": {
"keySpecs": [
"Passport Holder",
"Passport Wallet"
],
"detailedSpecs": [],
"specificationList": [
{
"key": "General",
"values": [
{
"key": "Type",
"value": [
"Passport Organizer"
]
},
{
"key": "Ideal For",
"value": [
"Men's, Women's"
]
},
{
"key": "Material",
"value": [
"Leather, Cloth"
]
},
{
"key": "Slot for Cards",
"value": [
"Yes"
]
},
{
"key": "Slot for Passport",
"value": [
"Yes"
]
},
{
"key": "Slot for Cheque Book",
"value": [
"Yes"
]
},
{
"key": "Style Code",
"value": [
"CDBP040739"
]
},
{
"key": "Color Code",
"value": [
"Black"
]
}
]
},
{
"key": "Warranty",
"values": [
{
"key": " ",
"value": [
"1 Year Newhide warranty"
]
}
]
}
],
"booksInfo": {
"language": null,
"binding": null,
"pages": null,
"publisher": null,
"year": 0,
"authors": []
},
"lifeStyleInfo": {
"sleeve": null,
"neck": null,
"idealFor": [
"Men's",
"Women's"
]
}
}
}
}
I and not able decode
"categorySpecificInfoV1": {
"keySpecs": [
"Passport Holder",
"Passport Wallet"
],
I have tried json_decode. This gives error
Warning: json_decode() expects parameter 1 to be string, array given.
$keySpecs=$product['categorySpecificInfoV1']['keySpecs'];
$ar = json_decode($keySpecs,true);
I am new to PHP, how can I print the keyspecs from keyspecs array?
The warning is pretty clear. You need to pass the entire string you obtained from Flipkart into the json_decode() function.
See Documentation
$ar = json_decode($flipkart_result, true);
print_r($ar); //to check the array.
print_r($ar['categorySpecificInfoV1']['keySpecs']);
This should do. Post more information if this does not help.