JSON to JSON Transformation using Jolt NIFI? - java

`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": ""
}
}
}
}
}
]

Related

Jolt - Self containing Object- Resursively replace field name

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[]"
}
}
}
}
}
}
}
}
]

Is there a mongodb query to merge fields in a specific mongodb document

So,suppose I have a document like :
{
"country": "USA",
"capital": "Washington DC",
"language": "English",
"other": {
"China": {
"capital": "Shanghai",
"language": "Chinese"
},
"Spain": {
"capital": "Madrid",
"language": "Spanish"
}
}
}
Now I want to return my document as:
{
"country": "USA",
"capital": "Washington DC",
"language": "English",
}
when I choose the country as "USA" but when I have the country set as "Spain", then I want my mongodb query to return the document as:
{
"country": "Spain",
"capital": "Madrid",
"language": "Spanish",
}
The fields should merge in accordance with the country specified in the "other" field.
How can I achieve this?
Try this one:
country = "Spain"
db.collection.aggregate([
// Make a uniform structure (why do you have 'USA' and 'other'?)
{ $replaceRoot: { newRoot: { $mergeObjects: ["$other", { USA: { capital: "$capital", language: "$language" } }] } } },
{ $project: { countries: { $objectToArray: "$$ROOT" } } },
{
$set: {
countries: {
$filter: {
input: "$countries",
cond: { $eq: ["$$this.k", country] }
}
}
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
{ country: { $first: "$countries.k" } },
{ $first: "$countries.v" }]
}
}
}
])

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