java script to parse an array from json - javascript

{
"took": 72,
"hits": {
"total": {
"value": 10000
}
},
"aggregations": {
"2": {
"buckets": [{
"key": "Perf",
"doc_count": 159874
}]
}
}
}
Could someone guide me to take the value of buckets

The built-in JSON should do the heavy lifting for you:
const str = '{ "took": 72, "hits": { "total": { "value": 10000 } }, "aggregations": { "2": { "buckets": [{ "key": "Perf", "doc_count": 159874 }] } } }';
const obj = JSON.parse(str);
the_arry = obj['aggregations']['2']['buckets'];

This should work:
var myrs = { "took": 72, "hits": { "total": { "value": 10000 } }, "aggregations": { "2": { "buckets": [{ "key": "Perf", "doc_count": 159874 }] } } };
for(var i in myrs["aggregations"]) {console.log(myrs["aggregations"][i]['buckets']);}

You can start by parsing the JSON string with var obj = JSON.parse(). It gives you an object representation of the string to work on. Log the object to the console to easily discover the structure console.log(obj); or use an online JSON viewer to do that.
Assuming the structure is fixed, the buckets array can be accessed by var buckets = obj.aggregations["2"].buckets. Notice the access of the property 2.

Related

How to add a new array to a JSON object without creating duplicate array brackets

These nested for loops loop through JSON object containing product categories and subcategories. It loops through these sending a URL parameter to a function to scrape a website according to the parameter. I cant find a way to add the returned arrays containing scraped data to the JSON object (according to its place in the nested loop) without having to create an empty array first and then use .push to populate it, thus duplicating the array brackets. The function returning the scraped data already add the data to an array, so I'm not sure how to go about this.
The following loops add the correct data to the JSON object, however the array brackets are tripled.
for (let i=0; i<obj.length; i++) {
let subcats = obj[i].subcategories;
for(let n=0; n<subcats.length; n++) {
if(!subcats.facetValueData) {
subcats[n].code.replace(/\s/g, '')
let productDataSub = []
subcats[n].productData = productDataSub
subcats[n].productData.push(await scrapeData(obj[i].subcategories[n].code))
} else {
let subcatsFinal = subcats[n].facetValueData
for(let p=0; p<subcatsFinal.length; p++) {
subcatsFinal[p].code.replace(/\s/g, '')
let productData = []
subcatsFinal[p].productData = productData
subcatsFinal[p].productData.push(await scrapeData(obj[i].subcategories[n].facetValueData[p].code))
}
}
}
}
The following is the JSON data being looped, the productData=[] is an example of where the productData array should go, but it is not present when the data is being looped. It is what I'm trying to achieve, but with the data populated inside. The "code" value is the parameter which is appended to a url to scrape that pages data.
[
{
"id": 0,
"categoryName": "Beauty",
"subcategories": [
{
"count": 2688,
"code": "/beauty/skin-care",
"name": "Skincare",
"facetValueData": [
{
"count": 682,
"code": "/beauty/skin-care/lotion-serum-and-essence",
"name": "Lotion Serum & Essence",
"productData": []
},
{
"count": 497,
"code": "/beauty/skin-care/moisturiser-and-mist",
"name": "Moisturiser & Mist",
"productData": []
},
{
"count": 354,
"code": "/beauty/skin-care/cleanser-and-exfoliation",
"name": "Cleanser & Exfoliation",
"productData": []
},
{
"count": 345,
"code": "/beauty/skin-care/mask-and-treatment",
"name": "Mask & Treatment",
"productData": []
}
]
},
{
"count": 1549,
"code": "/beauty/makeup",
"name": "Makeup",
"facetValueData": [
{
"count": 653,
"code": "/beauty/makeup/face",
"name": "Face",
"productData": []
},
{
"count": 460,
"code": "/beauty/makeup/makeup-lips",
"name": "Lips",
"productData": []
},
{
"count": 337,
"code": "/beauty/makeup/eyes",
"name": "Eyes",
"productData": []
},
{
"count": 124,
"code": "/beauty/makeup/makeup-cheeks",
"name": "Cheeks",
"productData": []
}
]
}
]
},
{
"id": 1,
"categoryName": "Electronics",
"subcategories": [
{
"count": 526,
"code": "/electronics/audio-devices",
"name": "Audio",
"facetValueData": [
{
"count": 153,
"code": "/electronics/audio-devices/speakers",
"name": "Speakers",
"productData": []
},
{
"count": 145,
"code": "/electronics/audio-devices/earphones",
"name": "Earphones",
"productData": []
},
{
"count": 142,
"code": "/electronics/audio-devices/headphones",
"name": "Headphones",
"productData": []
},
{
"count": 51,
"code": "/electronics/audio-devices/true-wireless-and-airpods",
"name": "True Wireless & Airpods",
"productData": []
}
]
}
]
}
]
Not sure if all that necessary but here is the async function which scraped the pages and returns the arrays:
async function scrapeData(queryParam) {
let pageCounter = 0;
var product = []
var pagination;
try {
do {
var nextPageLink = `currentPage=${pageCounter}&query=::cagCollectionPoint:Departure+from+Singapore:cagCategory:${queryParam}`
var nextUrl = url.concat(nextPageLink)
const response = await axios({
method: "GET",
url: nextUrl,
withCredentials: true,
headers: headers
})
console.log(nextUrl)
product.push(response.data["products"])
pagination = response.data["pagination"]["totalPages"]
pageCounter++;
// console.log(response.data.currentQuery.query.value)
} while (pageCounter<=0)
return product;
} catch (error) {
console.error(error)
}
}

Transform an object of objects to an array (special example)

I am trying to transform an object of objects into an array of objects. Basically I want to transform this:
sales_over_time_week: {
"1": {
"orders": 96,
"total": 270240
},
"2": {
"orders": 31,
"total": 74121
}
}
into this:
[
{name: 1, orders:96, total:270240},
{name:2, orders:31, total: 74121}
]
To just transform it normally I would do this
var myData = Object.keys(items).map(key => {
return items[key];
});
and it would give me
[
{1: {orders: 31, total: 74121}},
{2: {orders: 52, total: 180284}}
]
but my example is a bit special
You can use Object.entries with .map()
let data = {
"1": {
"orders": 96,
"total": 270240
},
"2": {
"orders": 31,
"total": 74121
}
};
let result = Object.entries(data).map(([key, value]) => ({name: key, ...value}));
console.log(result);
Try
Object.keys(data).map(k=> ({name:+k, ...data[k]}));
data = {
"1": {
"orders": 96,
"total": 270240
},
"2": {
"orders": 31,
"total": 74121
}
}
let r = Object.keys(data).map(k=> ({name:+k, ...data[k]}));
console.log(r);

Re-arrage JSON values from existing values

The JSON provided is kind of unstructured and doesn't meet many of my
requirements. I have tried this many ways but does take a very long time
when I provide 100,000 records
Implemented Code
for (var f in stack.data) {
var field = new Object();
for (var o in stack.data[f]['field_values']) {
field[stack.data[f]['field_values'][o]['field_name']] = stack.data[f]['field_values'][o]['value'];
}
stack.data[f]['field_values'] = field;
}
console.log(JSON.stringify(stack, null, 2));
Input JSON:
var stack = {
"data": [{
"id": 950888888073,
"name": "www.stackoverflow.com",
"field_values": [{
"field_name": "Newsletter?",
"value": true
},
{
"field_name": "Parent",
"value": 950888661
},
{
"field_name": "Birthday",
"value": "2018-04-29"
},
{
"field_name": "Related matter",
"value": 1055396205
},
{
"field_name": "Referral",
"value": "Don Ho"
},
{
"field_name": "Spouse",
"value": "Wo Fat"
}
]
}]
}
Expected Output:
{
"data": [
{
"id": 950888888073,
"name": "www.stackoverflow.com",
"field_values": {
"Newsletter?": true,
"Parent": "Gigi Hallow",
"Birthday": "2018-04-29",
"Related": "2012-00121-Sass",
"Referral": "Don Ho",
"Spouse": "Wo Fat"
}
Sometimes "field_values can be empty. Need to check them as well
{
"id": 950821118875,
"name": "www.google.com",
"field_values": [],
}
This is mostly re-arranging the values. Here values becomes keys. There should actually be one liner to handle this, but i am run out of options.
Hope the question is clear
It would probably help to declare a variable to hold the array element, rather than doing 4 levels of indexing every time through the loop. You can also use destructuring to extract the properties of the object.
And use {} rather than new Object.
Even if this doesn't improve performance, it makes the code easier to read.
var stack = {
"data": [{
"id": 950888888073,
"name": "www.stackoverflow.com",
"field_values": [{
"field_name": "Newsletter?",
"value": true
},
{
"field_name": "Parent",
"value": 950888661
},
{
"field_name": "Birthday",
"value": "2018-04-29"
},
{
"field_name": "Related matter",
"value": 1055396205
},
{
"field_name": "Referral",
"value": "Don Ho"
},
{
"field_name": "Spouse",
"value": "Wo Fat"
}
]
}]
}
for (var f in stack.data) {
const field = {};
const fobj = stack.data[f];
for (var o in fobj.field_values) {
const {field_name, value} = fobj.field_values[o];
field[field_name] = value;
}
fobj.field_values = field;
}
console.log(JSON.stringify(stack, null, 2));

Get exact data from json to react

I have this JSON file :
[{
"name": "bagette",
"price": "0.200"
}, {
"name": "farine",
"price": "1"
}, {
"name": "tomato",
"price": "1.200"
}, {
"name": "chocola",
"price": "4.000"
}]
I want to get the data from json file to an array in react for example :
console.log(data[0][0]); // bagette
console.log(data[0][1]); // 0.200
console.log(data[1][0]); // farine
console.log(data[3][1]); // 4.000
I'm a beginner in React Please can someone help me to write the code ?
var data = [{
"name": "bagette",
"price": "0.200"
}, {
"name": "farine",
"price": "1"
}, {
"name": "tomato",
"price": "1.200"
}, {
"name": "chocola",
"price": "4.000"
}];
data = data.map(val=>Object.values(val));
console.log(data[0][0]);
console.log(data[0][1]);
console.log(data[1][0]);
console.log(data[2][1]);
You can parse json into an object with JSON.parse(). Then you can map every object to an array. Note that this is the only way to totally ensure the order of the properties is the desired one as object properties have no guaranteed order.
const json = '[{"name": "bagette","price": "0.200"}, {"name": "farine","price": "1"},{"name":"tomato","price": "1.200"}, {"name": "chocola","price": "4.000"}]';
const data = JSON.parse(json);
const transformedData = data.map(obj => [obj.name, obj.price]);
console.log(transformedData[0][0]); // bagette
console.log(transformedData[0][1]); // 0.200
console.log(transformedData[1][0]); // farine
console.log(transformedData[3][1]); // 4.000
But I really don't know if that is a good idea. Why would you want to introduce magic numbers when you already have named properties to access in your dataset.
You can use Array.prototype.map() to return an array of array
var data = [{
"name": "bagette",
"price": "0.200"
}, {
"name": "farine",
"price": "1"
}, {
"name": "tomato",
"price": "1.200"
}, {
"name": "chocola",
"price": "4.000"
}];
data = data.map((x)=>[x.name , x.price]);
console.log(data[0][0]);
console.log(data[0][1]);
console.log(data[1][0]);
console.log(data[2][1]);
let arr = [{
"name": "bagette",
"price": "0.200"
}, {
"name": "farine",
"price": "1"
}, {
"name": "tomato",
"price": "1.200"
}, {
"name": "chocola",
"price": "4.000"
}]
let data = arr.map(item=>{
return [item.name, item.price]
})
That's you need?
Not exactly clear what you are trying to do
Yu can use
console.log(data[0].name) //bagette
console.log(data[0]['name']) //bagette
or to iterate through every property in object:
for(var propertyName in data[0]){
console.log(data[0][propertyName]);
}

Underscore.js : findWhere with nested property value

How do I go about the filtering below:
[{
"id": 100,
"title": "Tlt1",
"tax": [{
"name": "Tax1",
"id": 15
}, {
"name": "Tax1",
"id": 17
}]
}, {
"id": 101,
"title": "Tlt2",
"tax": [{
"name": "Tax2",
"id": 16
}]
}, {
"id": 102,
"title": "Tlt3",
"tax": [{
"name": "Tax3",
"id": 17
}, {
"name": "Tax3",
"id": 18
}]
}]
to get only those where tax.id is 17, as per below:
[
{
"id": 100,
"title": "Tlt1",
"tax": [
{
"name": "Tax1",
"id": 15
},
{
"name": "Tax1",
"id": 17
}
]
},
{
"id": 102,
"title": "Tlt3",
"tax": [
{
"name": "Tax3",
"id": 17
},
{
"name": "Tax3",
"id": 18
}
]
}
]
Currently I use the method below, but maybe there is more clean way of going about this?
var arr = [];
_(data).each(function (v1, k1) {
_(v1.tax).each(function (v2, k2) {
if (v2.id == id) {
arr.push(v1);
}
});
});
Demo here: http://jsfiddle.net/7gcCz/2/
Any suggestion much appreciated.
You may use the combination of _.filter and _.where
_.filter(data, function(obj) {
return _.where(obj.tax, {id: ID_TO_FIND}).length > 0;
})
See demo: http://jsfiddle.net/hCVxp/
Update
Thanks to #GruffBunny. A more efficient way is to use _.some to avoid looping through all tax items:
var arr = _.filter(data, function(obj) {
return _.some(obj.tax, {id: ID_TO_FIND});
});
See demo: http://jsbin.com/putefarade/1/edit?html,js,console
Use _.filter to find candidate items and _.some to check for the existence of an item in a collection:
var filteredList = _.filter(list, function(item){
return _.some(item.tax, { id: 17 });
});
You can do this with _.where alone.
var filteredList = _.where(list, { tax:[{id:17}] });
Edit: This method works in older version of lodash, but wont work in current underscore or lodash.
Working example anyway:
https://jsfiddle.net/rhp4crma/

Categories

Resources