I have a large JSON payload and I want to format, the specific object of the payload using JS.
I want flightdetails array object to edit and remove the special characters from it. How can I achieve this?
I have been working on this using XSLT for the past 2 days, and I went nowhere hence I decided to remove it using JS.
Example of the array(there can be more than 30 records inside the flightdetails)
"flightdetails": [
{
"id": XF-2092,
"trips": 2,
"categories": {
"flights": [
"\"return\",\"oneway\""
]
},
"id": XF-2093,
"trips": 1,
"categories": {
"flights": [
"\"return\""
]
}
}
]
Expected Output
"flightdetails": [
{
"id": XF-2092,
"trips": 2,
"categories": {
"flights": [
"return","oneway"
]
},
"id": XF-2093,
"trips": 1,
"categories": {
"flights": [
"return"
]
}
}
]
The flightdetails object inside the //destinations/flightdetails path
I am using pouchdb and have a document structure like this:
{
"_id": "1",
"title": "Title",
"categories": {
"category1": {
"salesx": [
{
"date": "2016-09-15 00:00:00",
"qty": "2"
},
{
"date": "2016-09-16 00:00:00",
"qty": "3"
}
],
"salesy": [
[
{
"date": "2016-09-15 00:00:00",
"qty": "1"
}
]
]
}
},
"_rev": "rev"
}
I now want to get something like this as a result from pouchdb:
{
"_id": "1",
"title": "Title",
"categories": {
"category1": {
"salesx": 2 + 3 = 5,
"salesy": [
[
{
"date": "2016-09-15 00:00:00",
"qty": "1"
}
]
]
}
},
"_rev": "rev"
}
So, salesx should be grouped and the qty should be summarized.
salesy should not be changed.
I also want to add a where clause to the date field.
I tried to start with a map function which selects the correct dates. But I don't know what to emit to group the qty later.
function myMapFunction(doc) {
doc.categories.forEach(category => {
category.salesx.forEach(salex => {
if (salex.date >= mydate) {
emit(?)
}
})
})
}
I can't find a good example on pouchdb docs. They only provide examples for grouping multiple documents together.
Does anyone know how I can achieve grouping the document?
I modified the documents like below, to convert the qty field from string values to number values, to be able to do math operations:
{
"_id": "sale0000",
"_rev": "2-5cd6fff48f9fd099481d3b523ff1191f",
"title": "Title",
"categories": {
"category1": {
"salesx": [
{
"date": "2016-09-15 00:00:00",
"qty": 2
},
{
"date": "2016-09-16 00:00:00",
"qty": 3
}
],
"salesy": [
[
{
"date": "2012-09-15 00:00:00",
"qty": 1
}
]
]
}
}
}
As you mentioned in the comments:
All documents look exactly like the one I already showed. Except the
number of entries in salesx and the number of categories (e.g.
category1, category2 ...) is modified
Based on your above statement, I created in my sample database some documents with varying number of entries in salesx and varying number of categoiry1, category2, etc., but each salesy has only one entry.
Here is another one of my sample documents:
{
"_id": "sale0002",
"_rev": "3-12bf5c872fc124c1f0a5a4a108cb4f7a",
"title": "Title",
"categories": {
"category1": {
"salesx": [
{
"date": "2016-09-15 00:00:00",
"qty": 2
},
{
"date": "2016-09-16 00:00:00",
"qty": 3
},
{
"date": "2017-10-16 00:00:00",
"qty": 9
}
],
"salesy": [
[
{
"date": "2014-09-15 00:00:00",
"qty": 1
}
]
]
},
"category2": {
"salesx": [
{
"date": "2018-01-15 00:00:00",
"qty": 2
},
{
"date": "2012-08-16 00:00:00",
"qty": 3
},
{
"date": "2015-10-16 00:00:00",
"qty": 7
}
],
"salesy": [
[
{
"date": "2011-03-15 00:00:00",
"qty": 1
}
]
]
}
}
}
I'm not sure if I understand what exactly you intend, but I developed the following view map function:
function (doc) {
if(doc.categories){
for(var categ in doc.categories){
var salesx_qty_sum=0;
for(var i=0, len=doc.categories[categ].salesx.length; i<len; i++){
salesx_qty_sum += doc.categories[categ].salesx[i].qty
}
emit(
doc.categories[categ].salesy[0][0]['date'], //emit the date of salesy as the key
//my understanding is that you need to index/sort
//according to date of salesy
[
salesx_qty_sum, //emit the sum of qty for salesx as value
doc.categories[categ].salesy[0][0]['qty'] //emit qty for salesy (unchanged) as value
]
);
}
}
}
In my sample database I created four documents like yours with different number of entries in salesx and also varying number of categories. There is only one entry inside each salesy:
$ curl -k -X GET 'https://admin:****#192.168.1.106:6984/sample/_all_docs'
{"total_rows":53,"offset":0,"rows":[
...
...
{"id":"_design/by_date","key":"_design/by_date","value":{"rev":"20-04cb881624aa819dac13f69c688f2124"}},
...
...
{"id":"sale0000","key":"sale0000","value":{"rev":"2-5cd6fff48f9fd099481d3b523ff1191f"}},
{"id":"sale0001","key":"sale0001","value":{"rev":"2-e8ee019fb0ad076b82d3f4772f736559"}},
{"id":"sale0002","key":"sale0002","value":{"rev":"3-12bf5c872fc124c1f0a5a4a108cb4f7a"}},
{"id":"sale0003","key":"sale0003","value":{"rev":"2-6d40368ddd16b9d02b908ea95d061c7e"}}
]}
The above view map (named by_date) is indexing my documents like below, in which the key is the date of salesy. Based your description, I assume there should be only one entry in each salesy in each category. Also, the value below is an array in which first element is sum of qty for salesx, and the second element is unchanged qty of salesy:
$ curl -k -X GET 'https://admin:admin#192.168.1.106:6984/sample/_design/by_date/_view/by_date'
{"total_rows":6,"offset":0,"rows":[
{"id":"sale0002","key":"2011-03-15 00:00:00","value":[12,1]},
{"id":"sale0000","key":"2012-09-15 00:00:00","value":[5,1]},
{"id":"sale0001","key":"2013-05-20 00:00:00","value":[12,1]},
{"id":"sale0002","key":"2014-09-15 00:00:00","value":[14,1]},
{"id":"sale0003","key":"2015-04-15 00:00:00","value":[5,1]},
{"id":"sale0003","key":"2018-04-05 00:00:00","value":[5,1]}
]}
You can query the by_date view with startkey and endkey to get the results between two dates, like this:
$ curl -k -X GET 'https://admin:admin#192.168.1.106:6984/sample/_design/by_date/_view/by_date?startkey="2013"&endkey="2015"'
{"total_rows":6,"offset":2,"rows":[
{"id":"sale0001","key":"2013-05-20 00:00:00","value":[12,1]},
{"id":"sale0002","key":"2014-09-15 00:00:00","value":[14,1]}
]}
I'm trying to make an event-based calendar by iterating through an array of json objects with individual dates using javascript.
What I want to achieve is a new structure from these objects, where i get the year from the object and make that a unique key which can contain multiple months as keys retrieved from the objects, which then again can contain multiple days as keys retrieved from the objects, which again contains multiple objects... phew..
F.ex., say I have this json file:
"objects": [
{
"dateTime": {
"date": "2016-12-24"
},
"values": {
"value1": "some value",
"value2": 700
}
}, {
"dateTime": {
"date": "2016-12-24"
},
"values": {
"value1": "other value",
"value2": 3100
}
}, {
"dateTime": {
"date": "2016-12-25"
},
"values": {
"value1": "random value",
"value2": 300
}
}, {
"dateTime": {
"date": "2017-01-02"
},
"values": {
"value1": "value",
"value2": 100
}
}]
Now I want to iterate through each object and make something like this:
[{
"2017": [{
"january": [{
"monday": [
object
]
}]
},
"2016": {
"december": [{
"saturday": [
object,
object
],
"sunday": [
object
]
}]
}]
}]
So I don't want any duplicates of any object which is set as key. What path should I go for? I've been looking into Map, WeakMap, Set and WeakSet, but I just can't figure out how to do this properly.
[
{
"May": [
{
"year": 1994,
"date": "2"
},
{
"Sequence": 2,
"Type": "Images"
}
],
"_id": "1122"
}
]
I can get id except "date" which is inside array. And date just show me undefined.
var data = [
{
"May": [
{
"year": 1994,
"date": "2"
},
{
"Sequence": 2,
"Type": "Images"
}
],
"_id": "1122"
}
];
alert(data[0].May[0]["date"]);
You should use arr[i].May[0]['date'], date is inside the object of the 0th index of array May.
Array will be start with the index 0 only . so from your json data 0th position only have the key named "date". So you should use like #void answer.
If you had doubt about whether data is have or not use like below
if(data[0].May[0]["date"])
alert(data[0].May[0]["date"]);
Thank you :)
I am using amazon product api, I'm searching for an item using ASIN number:
amazon = require('amazon-product-api');
var client = amazon.createClient({
awsId: "*",
awsSecret: "*",
awsTag: "Tag"
});
client.itemLookup({
idType: 'ASIN',
itemId: 'B00WA6ZL8S',
responseGroup :'Variations'
}).then(function(results) {
console.log(JSON.stringify(results));
}).catch(function(err) {
console.log(err);
});
The result is a long JSON file with more than 40000 line of code, here is some of it :
[{
"ASIN": ["B00WA6ZL8S"],
"ParentASIN": ["B00WA6ZL8S"],
"VariationSummary": [{
"LowestPrice": [{
"Amount": ["9200"],"CurrencyCode": ["USD"], "FormattedPrice": ["$92.00"]
}],
"HighestPrice": [{
"Amount": ["13995"],
"CurrencyCode": ["USD"],
"FormattedPrice": ["$139.95"]
}]
}],
"Variations": [{
"TotalVariations": ["48"],
"TotalVariationPages": ["1"],
"VariationDimensions": [{
"VariationDimension": ["Size", "Color"]
}],
"Item": [{
"ASIN": ["B00RHLICFA"],
"ParentASIN": ["B00WA6ZL8S"],
"SmallImage": [{
"URL": ["http://ecx.images-amazon.com/images/I/41z-SPDeICL._SL75_.jpg"],
"Height": [{
"_": "35",
"$": {
"Units": "pixels"
}
}],
"Width": [{
"_": "75",
"$": {
"Units": "pixels"
}
}]
}],
How do I get the value of Height or Width ... etc ? And what is the point of the square brackets in "VariationSummary": [{ or "Variations": [{ ?? I mean they are not arrays of objects! .
results[0].Variations[0].Item[0].SmallImage[0].Height[0]._
Sometimes objects are better organized inside an array. You can use array functions like push or pop to organize a large group of objects easier.
Edit, I forgot SmallImage[0] and item[0], corrected answer.