Jolt - Self containing Object- Resursively replace field name - java

I am pretty new to Jolt. I have a JSON payload that represents a logical condition such as AND/OR which can contain an array of conditions called "conditionPredicates". This can result in nested conditions of the form AND(OR(a, b ,c), OR(d,e)) I want to replace the field "conditionPredicates" as simply "conditions". Any help please?
Sample Input:
{
"type": "Condition.Aggregate.AND",
"payload": {
"conditionPredicates": [
{
"type": "Condition.Apple",
"payload": {
"fruit": "apple"
}
},
{
"type": "Condition.Aggregate.AND",
"payload": {
"conditionPredicates": [
{
"type": "Condition.Orange",
"payload": {
"fruit": "orange"
}
}
]
}
}
]
}
}
Expected Output
{
"type": "Condition.Aggregate.AND",
"payload": {
"conditions": [
{
"type": "Condition.Apple",
"payload": {
"fruit": "apple"
}
},
{
"type": "Condition.Aggregate.AND",
"payload": {
"conditions": [
{
"type": "Condition.Orange",
"payload": {
"fruit": "orange"
}
}
]
}
}
]
}
}

You can use a shift transformation like this one :
[
{
"operation": "shift",
"spec": {
"*": "&",
"payload": {
"conditionPredicates": {
"*": {
"*": "&3.conditions[&1].&",
"payload": {
"*": "&4.conditions[&2].&1.&",
"conditionPredicates": {
"*": {
"#": "&3.conditions[&4].&3.conditions[]"
}
}
}
}
}
}
}
}
]

Related

jolt transformation of this complex scenario?

I want the jolt transform for the given input . Your help in this is highly appreciated . thanks i am providing the input and expected output. in input json Photos array is dynamic in nature. Here it is 3 , it can be 3 or 4 or5 any .
Input JSON :
{
"Entity": {
"card": {
"cardNo":"123456789",
"cardStatus":"10",
"cardAddress":"UK",
"cardAddress1":"US",
"cardCity":"mk" ,
"name": "RAM",
"lastName": "ABU",
"name1": "RAM1",
"lastName1": "ABU1"
},
"Photos": [
{
"Id": 327703,
"Caption": "TEST>> photo 1",
"Url": "http://bob.com/0001/327703/photo.jpg"
},
{
"Id": 327704,
"Caption": "TEST>> photo 2",
"Url": "http://bob.com/0001/327704/photo.jpg"
},
{
"Id": 327704,
"Caption": "TEST>> photo 2",
"Url": "http://bob.com/0001/327704/photo.jpg"
}
]
}
}
Used Jolt Spec :
[
{
"operation": "shift",
"spec": {
"Entity": {
"card": {
"cardNo": "tab.text",
"cardAddress": "address[0].add",
"cardAddress1": "address[0].add2",
"cardC*": "address[0].mk",
"Id1": "Photos.no",
"#http.1.com": "Photos.caption2",
"Id2": "Photos.no",
"#http.2.com": "Photos.caption2"
},
"Photos": {
"*": {
"Id": "Photos.no",
"Caption": "Photos.caption2"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"tab": "&",
"address": "&",
"Photos": {
"*": {
"*": {
"#": "&3[&1].&2"
}
}
}
}
}
]
Current Output :
{
"tab": {
"text": "123456789"
},
"address": [
{
"add": "UK",
"add2": "US",
"mk": "mk"
}
],
"Photos": [
{
"caption2": "http.1.com",
"no": 222444
},
{
"caption2": "http.2.com",
"no": 222444
},
{
"caption2": "TEST>> photo 1",
"no": 327703
},
{
"caption2": "TEST>> photo 2",
"no": 327704
},
{
"caption2": "TEST>> photo 2",
"no": 327704
}
]
}
Expected Output ( i need "no" in string like below ) :
{
"tab": {
"text": "123456789"
},
"address": [
{
"add": "UK",
"add2": "US",
"mk": "mk"
}
],
"Photos": [
{
"caption2": "http.1.com",
"no": "222444"
},
{
"caption2": "http.2.com",
"no": "222444"
},
{
"caption2": "TEST>> photo 1",
"no": "327703"
},
{
"caption2": "TEST>> photo 2",
"no": "327704"
},
{
"caption2": "TEST>> photo 2",
"no": "327704"
}
]
}
You can add a modify transformation to the current spec along with toString function such as
,
{
"operation": "modify-overwrite-beta",
"spec": {
"Photos": {
"*": {
"no": "=toString"
}
}
}
}

JSON to JSON Transformation using Jolt NIFI?

`I have the following input json format and need to convert the below json file.Iam using jolt transformation but not able to properly format the output
Iam using https://jolt-demo.appspot.com/#inception website
{
"School": {
"Data": {
"Id": "",
"Time": "",
"Txs": "",
"Pty": {
"Name": "",
"Number": {
"OID": {
"REM": {
"sequenced": ""
}
}
}
}
},
"Facebook": [
{
"fid": {
"fedit": "",
"fcopy": ""
},
"Details": "Further details",
"FBData": {
"Flp": {
"Fst": {
"foo": "bar"
}
}
}
}
]
}
}
`Need the following output json format using JOLT Iam trying to convert using the jolt transformation
{
"School": {
"Data": {
"Id": "",
"Time": "",
"Txs": "",
"Pty": {
"Name": "",
"ONum": {
"OID": {
"REM": {
"sid": ""
}
}
}
}
},
"Rollingstone": [
{
"rollid": {
"fid": {
"famt": "",
"ofaid": ""
},
"status": "Login",
"statusDescription": "Login Successfully",
"ADDinfo": ""
}
}
]
}
}
You can use following successive specs
[
{
"operation": "shift",
"spec": {
"*": {
"Data": {
"*": "&2.&1.&",
"Pty": {
"*": "&3.&2.&1.&",
"Number": {
"*": {
"*": {
"*": "&6.&5.&4.&3.&2.&1.sid"
}
}
}
}
},
"Facebook": {
"*": {
"fid": {
"fedit": "Rollingstone[0].rollid.&1.famt",
"fcopy": "Rollingstone[0].rollid.&1.ofaid"
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"Rollingstone": {
"*": {
"rollid": {
"status": "Login",
"statusDescription": "Login Successfully",
"ADDinfo": ""
}
}
}
}
}
]

How can I map inner fields to the same array object in jolt?

Having this input json:
{
"orderItems": [
{
"itemName": "Mozzz",
"quantity": 1
},
{
"itemName": "zer",
"quantity": 0,
"bar": {
"arr": [
{
"meta_itemName": "Small Barqs2",
"meta_quantity": 22
}
]
}
}
]
}
I'm trying to shift the "meta_" prefixed fields up inside the orderItems object array, eliminating the bar and arr keys.
I tried this spec:
[
{
"operation": "shift",
"spec": {
"orderItems": {
"*": {
"quantity": "basket_item[#2].quantity",
"itemName": "basket_item[#2].itemName",
"bar": {
"arr": {
"*": {
"meta_itemName": "basket_item[#2].m2",
"meta_quantity": "basket_item[#2].m3"
}
}
}
}
}
}
}
]
but the fields renamed to m2 and m3 don't stay inside the "zer" item.
This spec should work for you (tested with https://jolt-demo.appspot.com/)
[
{
"operation": "shift",
"spec": {
"orderItems": {
"*": {
"bar": {
"arr": {
"*": { "meta_*": "orderItems[&4].&" }
}
},
"*": "orderItems[&1].&"
}
}
}
}
]
output for your input:
{
"orderItems" : [ {
"itemName" : "Mozzz",
"quantity" : 1
}, {
"itemName" : "zer",
"quantity" : 0,
"meta_itemName" : "Small Barqs2",
"meta_quantity" : 22
} ]
}

How to term query nested json objects/fields in elastic search?

I am doing term aggregation based on field [type] like below but elastic is returning only 1 term count instead of 2 it is not doing nested object aggregation i.e under comments.data.comments[is a list] under this i have 2 type.
{
"aggs": {
"genres": {
"terms": {
"field": "comments.data.comments.type"
}
}
}
}
Gotta utilize the nested field type:
PUT events
{
"mappings": {
"properties": {
"events": {
"type": "nested",
"properties": {
"ecommerceData": {
"type": "nested",
"properties": {
"comments": {
"type": "nested",
"properties": {
"recommendationType": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
}
POST events/_doc
{
"events": [
{
"eventId": "1",
"ecommerceData": [
{
"comments": [
{
"rank": 1,
"recommendationType": "abc"
},
{
"rank": 1,
"recommendationType": "abc"
}
]
}
]
}
]
}
GET events/_search
{
"size": 0,
"aggs": {
"genres": {
"nested": {
"path": "events.ecommerceData.comments"
},
"aggs": {
"nested_comments_recomms": {
"terms": {
"field": "events.ecommerceData.comments.recommendationType"
}
}
}
}
}
}

Java Jolt bazaarvoice nested array

I am using Jolt to transform data from
{
"Data": {
"ROOT": {
"MODIFIED_DATE": "2018-06-27T13:53:47.8",
"A1": [
{
"FLD1": "BB",
"A2": [
{
"FLD2": 1
}
]
},
{
"FLD1": "AA",
"A2": [
{
"FLD2": 2
}
]
}
]
}
},
"metaData": {
"FLD3": "5f3c4"
}
}
To
{
"modifiedDate": "2018-06-27T13:53:47.8",
"a1": [
{
"fld1": "BB",
"a2": [
{
"fld2": 1
}
]
},
{
"fld1": "AA",
"a2": [
{
"fld2": 2
}
]
}
],
fld3: "5f3c4"
}
My spec is
[
{
"operation": "shift",
"spec": {
"Data": {
"ROOT": {
"MODIFIED_DATE": "modifiedDate",
"A1": {
"*": {
"FLD1": "a1[&1]",
"A2": {
"*": {
"FLD2": "a2[&2].fld2"
}
}
}
}
}
},
"metaData": {
"FLD3": "fld3"
}
}
},
{
"operation": "default",
"spec": {}
}
]
But it doesn't work properly. What have i missed?
Figured it out.
[
{
"operation": "shift",
"spec": {
"Data": {
"ROOT": {
"MODIFIED_DATE": "modifiedDate",
"A1": {
"*": {
"FLD1": "a1[&1].fld1",
"A2": {
"*": {
"FLD2": "a1[&3].a2[&1].fld2"
}
}
}
}
}
},
"metaData": {
"FLD3": "fld3"
}
}
},
{
"operation": "default",
"spec": {}
}
]

Categories

Resources