Transform Object into array containing the Object - jolt

I would like to wrap an object into an array using JoLT specification, for further use in NiFi.
But I cannot seem to get the solution.
Let's say I have this json as input
{
"toIndex": [
{
"content": [
{
"firstName": "maxime",
"lastName": "g",
"toArray": {
"one": "fun",
"two": "funfun",
"three": "funfunfun"
}
}
]
},
{
"content": [
{
"firstName": "monica",
"lastName": "beluci",
"toArray": {
"one": "fun",
"two": "funfun",
"three": "funfunfun"
}
}
]
}
]
}
I would very much like to get this as output :
{
"toIndex": [
{
"content": [
{
"firstName": "maxime",
"lastName": "g",
"toArray": [ {
"one": "fun",
"two": "funfun",
"three": "funfunfun"
}]
}
]
},
{
"content": [
{
"firstName": "monica",
"lastName": "beluci",
"toArray": [{
"one": "fun",
"two": "funfun",
"three": "funfunfun"
}]
}
]
}
]
}
Unfortunately I get this output :
{
"toIndex" : [ {
"content" : [ {
"firstName" : "maxime",
"lastName" : "g",
"toArray" : {
"one" : "fun",
"two" : "funfun",
"three" : "funfunfun"
}
}, {
"toArray" : [ {
"one" : "fun",
"two" : "funfun",
"three" : "funfunfun"
} ]
}, {
"firstName" : "monica",
"lastName" : "beluci",
"toArray" : {
"one" : "fun",
"two" : "funfun",
"three" : "funfunfun"
}
}, {
"toArray" : [ {
"one" : "fun",
"two" : "funfun",
"three" : "funfunfun"
} ]
} ]
} ]
}
My (miserable) spec goes like this :
[{
"operation": "shift",
"spec": {
"toIndex": {
"*": {
"content": {
"*": {
"#": "toIndex[&1].content[]", // passthru
"toArray": "toIndex[&1].content[].toArray[]"
}
}
}
}
}
}
]
If one of you be so kind as to indicate what I am doing wrong...
Thanks.

See if the cardinality operation is what you are looking for:
[
{
"operation": "cardinality",
"spec": {
"toIndex": {
"*": {
"content": {
"*": {
"toArray": "MANY"
}
}
}
}
}
}
]
Output
{
"toIndex" : [ {
"content" : [ {
"firstName" : "maxime",
"lastName" : "g",
"toArray" : [ {
"one" : "fun",
"two" : "funfun",
"three" : "funfunfun"
} ]
} ]
}, {
"content" : [ {
"firstName" : "monica",
"lastName" : "beluci",
"toArray" : [ {
"one" : "fun",
"two" : "funfun",
"three" : "funfunfun"
} ]
} ]
} ]
}

Related

MongoDb : query to match in same embedded document

Help me to match in single embedded document of both conditions.
db.inventory.insertOne([
... { "item": "journal", "instock": [ { "warehouse": "A", "qty": 5 }, { "warehouse": "C", "qty": 15 } ] },
... { "item": "notebook", "instock": [ { "warehouse": "C", "qty": 5 } ] },
... { "item": "paper", "instock": [ { "warehouse": "A", "qty": 60 }, { "warehouse": "B", "qty": 15 } ] },
... { "item": "planner", "instock": [ { "warehouse": "A", "qty": 40 }, { "warehouse": "B", "qty": 5 } ] },
... { "item": "postcard", "instock": [ { "warehouse": "B","qty": 15 }, { "warehouse": "C", "qty": 35 } ] }
... ])
Expected to return single document which match exactly as queried but returns two.
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )
{ "_id" : ObjectId("63061a1bb87c1278047a2717"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("63061a1bb87c1278047a271a"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
You need to perform the following query:
db.inventory.find({
instock: { $elemMatch: { qty: 5, warehouse: 'A' } }
});
Documentation for reference: https://www.mongodb.com/docs/v5.0/tutorial/query-array-of-documents/#a-single-nested-document-meets-multiple-query-conditions-on-nested-fields

Pass Array as Query Param WireMock

I need to pass the following array as query paramsin wiremock.Is there any possibility of doing that?
"Person": [
{
"name": "AAA",
"age": "20"
},
{
"name": "BBB",
"age": "26"
}
]
I have tried
"bodyPatterns" : [
{
"matchesJsonPath" : "$[?(#.Person.size() >= 2)]"
}
]
and
"bodyPatterns" : [
{
"matchesJsonPath" : "$.Person.name"
},
{
"matchesJsonPath" : "$.Person.age"
}
]

Need with Jolt Spec for dynamic key

My input looks like below
{
"honda" : {
"accord" : [ "30", "20" ],
"plus" : [ "20", "10", "" ]
},
"tesla" : {
"modelY" : [ "50", "20", "" ],
"modelX" : [ "20", "" ]
}
}
I want to write a spec to get only the first value in the array for each model. This is dynamic data and may vary depending on the query. The output must look something like this. I have tried an no luck with the dynamic key.
{
"honda" : {
"accord" : "30",
"plus" : "20"
},
"tesla" : {
"modelY" : "50",
"modelX" : "20"
}
}
Check this spec,
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"0": "&2.&1"
}
}
}
}
]

JSON conversion using JOLT

I am trying to convert a JSON to different format using JOLT (using NiFi JoltTransformJson processor). For single JSON record, the JOLT am using is working fine in JOLT app demo whereas if i execute with multiple JSON records then I am not getting expected output in JOLT app demo. Could anyone correct me what additional changes I need to do in JOLT spec to handle multiple JSON records.
sample input json
[
{
"pool": {
"field": [
{
"name": "BillingDay",
"value": "12"
},
{
"name": "Custom1",
"value": "POOL_BASE_PLAN_3GB"
}
]
},
"usage": {
"version": "3",
"quota": {
"name": "POOL_TOP_UP_1GB_2",
"cid": "5764888998010953848"
}
},
"state": {
"version": "1",
"property": [
{
"name": "SMS_RO_TOP",
"value": "1"
},
{
"name": "BillingTimeStamp",
"value": "2020-06-12T01:00:05"
},
{
"name": "timereset",
"value": "2020-01-12T00:35:53"
}
]
}
},
{
"pool": {
"field": [
{
"name": "PoolID",
"value": "111100110000003505209"
},
{
"name": "BillingDay",
"value": "9"
}
]
},
"usage": {
"version": "3"
},
"state": {
"version": "1",
"property": [
{
"name": "BillingTimeStamp",
"value": "2020-06-09T01:00:05"
},
{
"name": "timereset",
"value": "2019-03-20T17:10:38"
}
]
}
}
]
JOLT using:
[
{
"operation": "modify-default-beta",
"spec": {
"state": {
"property": {
"name": "NOTAVAILABLE"
}
},
"usage": {
"quota": {
"name": "NOTAVAILABLE"
}
}
}
},
{
"operation": "shift",
"spec": {
"pool": {
"field": {
"*": {
"value": "pool_item.#(1,name)"
}
}
},
// remaining elements print as it is
"*": "&"
}
}
]
Expected output JSON:
[
{
"pool_item" : {
"BillingDay" : "12",
"Custom1" : "POOL_BASE_PLAN_3GB"
},
"usage" : {
"version" : "3",
"quota" : {
"name" : "POOL_TOP_UP_1GB_2",
"cid" : "5764888998010953848"
}
},
"state" : {
"version" : "1",
"property" : [ {
"name" : "SMS_RO_TOP",
"value" : "1"
}, {
"name" : "BillingTimeStamp",
"value" : "2020-06-12T01:00:05"
}, {
"name" : "timereset",
"value" : "2020-01-12T00:35:53"
} ]
}
},
{
"pool_item" : {
"BillingDay" : "9",
"PoolID" : "111100110000003505209"
},
"usage" : {
"version" : "3",
"quota" : {
"name" : "NOTAVAILABLE"
}
},
"state" : {
"version" : "1",
"property" : [ {
"name" : "SMS_RO_TOP",
"value" : "1"
}, {
"name" : "BillingTimeStamp",
"value" : "2020-06-12T01:00:05"
}, {
"name" : "timereset",
"value" : "2020-01-12T00:35:53"
} ]
}
}
]
This below jolt shift specification will work for your multiple json's in input array.
[
{
"operation": "shift",
"spec": {
"*": {
"pool": {
"field": {
"*": {
"value": "[&4].pool_item.#(1,name)"
}
}
},
"usage": "[&1].usage",
"state": "[&1].state"
}
}
}
]

Mongo Filter Query Nested with mulitple and

Following is my query does exactly match with my document but still not getting output.Don't know why. Following is the document as well.
db.getCollection("analytics").find(
{
"$and" : [
{
"archive" : false
},
{
"platform" : "WEB"
},
{
"vendorId" : "3c7adbfe-14d7-4b26-9134-7e05d56573cc"
},
{
"createdAt" : {
"$gte" : 1578268800000.0
}
},
{
"createdAt" : {
"$lte" : 1580860800000.0
}
},
{
"$and" : [
{
"data.mobile" : "123"
},
{
"page" : "Loan Application"
},
{
"event" : "click"
}
]
},
{
"$and" : [
{
"data.aadharNumber" : "123"
},
{
"page" : "Personal Information"
},
{
"event" : "click"
}
]
},
{
"$and" : [
{
"data.totalExp" : "5"
},
{
"page" : "Professional Information"
},
{
"event" : "click"
}
]
}
]
}
);
Documents :
[
{
"page": "Loan Application",
"event": "click",
"loggedIn": true,
"vendorId": "3c7adbfe-14d7-4b26-9134-7e05d56573cc",
"data": {
"first": "Praveen",
"mobile": "1234"
},
"platform": "WEB"
},
{
"page": "Personal Information",
"event": "click",
"loggedIn": true,
"vendorId": "3c7adbfe-14d7-4b26-9134-7e05d56573cc",
"data": {
"panNumber": "123",
"aadharNumber": "123"
},
"platform": "WEB"
},
{
"page": "Professional Information",
"event": "click",
"loggedIn": true,
"vendorId": "3c7adbfe-14d7-4b26-9134-7e05d56573cc",
"data": {
"totalExp": "5"
},
"platform": "WEB"
}
]
There are a lot of issues going on with your query, you can try below query to return all documents :
db.getCollection("analytics").find({
$expr: {
$and: [
{
$eq: [
"$platform",
"WEB"
]
},
{
$eq: [
"$vendorId",
"3c7adbfe-14d7-4b26-9134-7e05d56573cc"
]
},
{
$or: [
{
"$and": [
{
"data": {
"mobile": "123"
}
},
{
"page": "Loan Application"
},
{
"event": "click"
}
]
},
{
"$and": [
{
"data": {
"aadharNumber": "123"
}
},
{
"page": "Personal Information"
},
{
"event": "click"
}
]
},
{
"$and": [
{
"data": {
"totalExp": "5"
}
},
{
"page": "Professional Information"
},
{
"event": "click"
}
]
}
]
}
]
}
})
Test : MongoDB-Playground