How to assign an array value to an object using JOLT? - arrays

I'm trying to flatten a json file and convert array elements to objects and assign those object values from another array. I will post both desired output and my current spec in a response:
Here's my input (required output in response):
{
"information": {
"Id": "2",
"cId": "P2",
"sId": 11,
"dataFrom": "4/15/2018T6:31:02Z",
"dataTo": "4/15/2018T6:42:02Z"
},
"description": {
"indicator": "SomeName",
"details": {
"headers": [
"id",
"aId",
"NAME4"
],
"values": [
[
1609,
12,
"NAME3"
],
[
1610,
11,
"NAME2"
]
]
}
}
}```

Related

In MongoDB I am searching in Nested Object Array but the result it is showing not appropriate

I am using collection Name "History" having below data
[
{
"_id": {
"$oid": "634a9d1b269c99e9364e8750"
},
"marks": [
{
"results": [
{
"product": "Abc",
"score": 55
}
]
}
]
},
{
"_id": {
"$oid": "634a9fae269c99e9364e8755"
},
"marks": [
{
"results": [
{
"product": "Abc",
"score": 10
},
{
"product": "Xyz",
"score": 5
}
]
}
]
},
{
"_id": {
"$oid": "634a9fae269c99e9364e8756"
},
"marks": [
{
"results": [
{
"product": "Abc",
"score": 8
},
{
"product": "Xyz",
"score": 7
}
]
}
]
},
{
"_id": {
"$oid": "634a9fae269c99e9364e8757"
},
"marks": [
{
"results": [
{
"product": "Abc",
"score": 7
},
{
"product": "Xyz",
"score": 8
}
]
}
]
}
]
My Fetch command is this..
db.History.findOne({"marks.results.product":"Xyz"})
command executes without an error but it shows wrong results..
{
"_id": {
"$oid": "634a9fae269c99e9364e8755"
},
"marks": [
{
"results": [
{
"product": "Abc",
"score": 10
},
{
"product": "Xyz",
"score": 5
}
]
}
]
}
The 1st object in results (product:"Abc") should not display as its not meet the criteria (Product="Xyz")
please correct me and guaid me how to fetch desired data (objects only meet criteria i.e. Product="Xyz" )
Your sample data here is a collection with 4 documents. 3 of these contain product="Xyz" along with other products. findOne finds one document that has product="Xyz" inside it, and return the document as is (without manipulating it).
What you are requesting is to get back only a part of the document - meaning you want to manipulate the (double) nested array results in the returned answer. You can do it using an aggregation pipeline.
One option is to use $unwind for this
db.collection.aggregate([
{$match: {"marks.results.product": "Xyz"}},
{$limit: 1} // if you want only one document to return
{$unwind: "$marks"},
{$unwind: "$marks.results"},
{$match: {"marks.results.product": "Xyz"}}
])
See how it works on the playground example - unwind
Another option is to $map and $filter:
See how it works on the playground example - filter

How to get value from nested JSON object in a generic way in Java

I want to fetch value of Column, Tag & Status attribute from below json string in a generic way.
[
{
"HoldTag": {
"Employee": {
"Column": "0",
"Tags": [
{
"Tag": "2345"
}
],
"Status": "3",
},
"Flag": "true"
}
}
]

Use Jolt to flatten an array of objects which contain array

Can Jolt flatten an array of objects which contain array? For example, is there a way to write Jolt transformation specs for the following input and output?
Jolt https://github.com/bazaarvoice/jolt
Input:
[
{"Id": "111", ["mobile": "1111", "home": "1112"]},
{"Id": "222", ["mobile": "2221"]}
]
Output:
[
{"Id": "111", "mobile": "1111"},
{"Id": "111", "home": "1112"},
{"Id": "222", "mobile": "2221"}
]
I didn't find a way to represent the output array indexes.
assuming input is fixed as
[
{
"Id": "111",
"Val": [
{
"mobile": "1111"
},
{
"home": "1112"
}
]
},
{
"Id": "222",
"Val": [
{
"mobile": "2221"
}
]
}
]
then applying the following transformation
[
{
"operation": "shift",
"spec": {
"*": {
"Id": null,
"Val": {
"*": {
"#": "[&3].[&0]",
"#(2,Id)": "[&3].[&0].id"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[]"
}
}
}
]
reach the expected output:
[ {
"mobile" : "1111",
"id" : "111"
}, {
"home" : "1112",
"id" : "111"
}, {
"mobile" : "2221",
"id" : "222"
} ]

Is there a way to deliver an array via a REST-Webservice in Denodo?

I´m importing a JSON-Datasource in Denodo which contains 2 arrays. In order to work with the data i flatten those arrays. However when delivering the data I want to get back to the initial array structure to get something like
{
"name": "name_of_my_view",
"elements": [
{
"result": [
{
"id": 40033495,
"first_name": Max,
"last_name": Mustermann
},
{
"id": 39960791,
"first_name": "Markus",
"last_name": "Markwart"
}
],
"took_ms": 4,
"result_count": 323,
"errors": [
{}
]
}
],
"links": [
{
"rel": "self",
"href": "https://address"
}
]
}
I have flattend both arrays (result, errors) in order to edit the respective fealds within them. However i only see the option of using UNION to combine them. If i do so I end up having all fealds in one hierarchy like (Ignore the sorting in this example) Oh and note that "code" and "description" are within the "error" array and are not shown in the above example because there are no errors in it:
{
"name": "name_of_my_view",
"elements": [
{
"took_ms": 4,
"result_count": 323,
"code": null,
"description": null,
"id": null,
"first_name": null,
"last_name": null
},
{
"took_ms": 4,
"result_count": 323,
"code": null,
"description": null,
"id": 40033495,
"first_name": null,
"last_name": null
}
],
"links": [
{
"rel": "self",
"href": "https://address"
}
]
}

Get inner array value based on condition with other values in mongoose

I have a DB structure as follows:
{
"Title": "AAA",
"Photos": ["/aaa/aaa.png"],
"Loc": "XXX",
"Emp": "SSS",
"Rate": [{
"Rating": 2,
"RateID": "12345654654",
"RatedDate": new Date()
}],
"Fav": [{
"FavValue": 2,
"FavID": "1111",
"FavDate": new Date()
}]
}
Here, I want to fetch all the details by default. But from array, i need to get the object that matches the ID.
In "Rate" array it needs to match "RateID" and in "Fav" array it need to match "FavID".
Both "Rate" and "Fav" wont contains objects always.
I have tried foll mongoose aggregate:
ss.aggregate([
{ $unwind: "$Rate" },
{
$group: {
"_id": '$_id',
"Title": { "$first": "$Title" },
"Loc": { "$first": "$Loc" },
"Emp": { "$first": "$Emp" },
"Photos": { "$first": "$Photos" },
"Rate": { $max: { $cond: [ { $eq: [ "$Rate.RateID", new ObjectId(id) ] }, '$Rate.Rating', null ] } },
"Fav": { $max: { $cond : [ { $eq : [{ "$size": "$Fav" }, 0]}, null, { $cond: [ { $eq: [ "$Fav.FavID", new ObjectId(id) ] }, '$Fav.FavValue', null ] } ]} }
}
}
], function (err, AvgResult) {
res.json({"result": AvgResult});
});
For "Rate", if the array is empty it returns empty result as {}
For "Fav", it returns null always if the array is empty or if it contains object
If I send the ID as '12345654654', the result should be like
{
"Title": "AAA",
"Photos": ["/aaa/aaa.png"],
"Loc": "XXX",
"Emp": "SSS",
"Rate": 2,
"Fav": null
}
If the ID is "1111", the result should be
{
"Title": "AAA",
"Photos": ["/aaa/aaa.png"],
"Loc": "XXX",
"Emp": "SSS",
"Rate": null,
"Fav": 2
}
If "Fav" is empty array in DB like "Fav": [] and ID - is "12345654654", then the result should be as follows
{
"Title": "AAA",
"Photos": ["/aaa/aaa.png"],
"Loc": "XXX",
"Emp": "SSS",
"Rate": 2,
"Fav": null
}
can any one help to get the expected result in all the above scenario..

Resources