Jolt trasform from array to object and extract value - json

I need to perform a Jolt transformation on the below example json:
Input
[
{
"DOCUMENT_HEADER_ID": "29120",
"Descrizione": "GAS",
"PrezzoTotale": "8.51"
},
{
"DOCUMENT_HEADER_ID": "29120",
"Descrizione": "IMPOSTE",
"PrezzoTotale": "7.25"
},
...
]
I need to trasform to
{
"DOCUMENT_HEADER_ID" : "29120",
"invoice" : [
{
"DOCUMENT_HEADER_ID" : "29120",
"Descrizione" : "GAS",
"PrezzoTotale" : "8.51"
}, {
"DOCUMENT_HEADER_ID" : "29120",
"Descrizione" : "IMPOSTE",
"PrezzoTotale" : "7.25"
},
...
]
}
Using this spec
[
{
"operation": "shift",
"spec": {
"#": "invoice"
}
}
]
I have this output:
{
"invoice" : [ {
"DOCUMENT_HEADER_ID" : "29120",
"Descrizione" : "GAS",
"PrezzoTotale" : "8.51"
}, {
"DOCUMENT_HEADER_ID" : "29120",
"Descrizione" : "IMPOSTE",
"PrezzoTotale" : "7.25"
},
...
]
}
but I can not figure out how to extract the value of DOCUMENT_HEADER_ID from the first element of original array

Can do it in a single shift.
[
{
"operation": "shift",
"spec": {
"#": "invoice",
"0": {
"DOCUMENT_HEADER_ID": "DOCUMENT_HEADER_ID"
}
}
}
]

I found the correct spec
[
{
"operation": "shift",
"spec": {
"#": "invoice"
}
},
{
"operation": "shift",
"spec": {
"invoice": {
"0": {
"DOCUMENT_HEADER_ID": "DOCUMENT_HEADER_ID"
},
"#": "invoice"
}
}
}
]

If having to retrieve single element, data like :
"array_field": ["array_single_element"]
can use:
{
"operation": "modify-overwrite-beta",
"spec": {
"array_field": "=concat(#(1,array_field))",
"*": "=concat(#(1,&))"
}
}

Related

JOLT Specification to Transform field value to field name

I am trying to convert below json payload into a json payload which has field name as the value of the field:
Can you please help to provide jold specification for this ?
Input JSON Payload :
{
"action": {
"allowPartialSuccess": false,
"records": [
{
"recordid": "a4c6364e-4446-47d0-b014-c20ca014fdb3",
"ShipToCustomerTextualAddress__c": "TestAddress1",
"ResellerPO__c": "TestAddress1",
"InvoiceCDBID__c": "TestAddress1"
},
{
"recordid": "73781a94-9660-4f69-9bde-f2bf1991317d",
"ShipToCustomerTextualAddress__c": "TestAddress2",
"ResellerPO__c": "TestAddress2",
"InvoiceCDBID__c": "TestAddress2"
}
],
"type": "update"
}
}
Desired Output Payload :
{
"action": {
"allowPartialSuccess": false,
"records": {
"a4c6364e-4446-47d0-b014-c20ca014fdb3": {
"ShipToCustomerTextualAddress__c": "TestAddress1",
"ResellerPO__c": "TestAddress1",
"InvoiceCDBID__c": "TestAddress1"
},
"73781a94-9660-4f69-9bde-f2bf1991317d": {
"ShipToCustomerTextualAddress__c": "TestAddress2",
"ResellerPO__c": "TestAddress2",
"InvoiceCDBID__c": "TestAddress2"
}
},
"type": "update"
}
}
This will help you resolve it.
#(level,attribute) -->does the work.
[
{
"operation": "shift",
"spec": {
"action": {
"*": "action.&",
"records": {
"*": {
"ShipToCustomerTextualAddress__c": "action.records.#(1,recordid).&",
"ResellerPO__c": "action.records.#(1,recordid).&",
"InvoiceCDBID__c": "action.records.#(1,recordid).&"
}
}
}
}
}
]
You can think symbolically through use of ampersands to replicate the respective values within a shift transformation, and a remove transformation in order to get rid of the undesired attribute(recordid) at the end such as
[
{
"operation": "shift",
"spec": {
"*": {
"records": {
"*": "&2.&1.#(0,recordid)" // &2(going two levels up) stands for replicating "action", &1 is for "records"
},
"*": "&1.&" // which represents the "else" case(eg. all but the "records" object); &1 is for "action", and & is for "records"
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"*": {
"*": {
"recordid": ""
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
You would use this for solution this problem.
[
{
"operation": "shift",
"spec": {
"action": {
"*": "action.&",
"records": {
"*": "action.records.#recordid"
}
}
}
}
]

Jolt specification to transform json into name value pair

I am trying to convert below JSON into name value pair :
{
"Size": "2",
"done": "true",
"records": [
{
"Id": "a7g6s0000004GZuAAM",
"NN": "00096411.0",
"Name": "ISOLIN TRADE & INVEST",
"RecordType": {
"attributes": {
"type": "TestType"
},
"Name": "Term"
}
},
{
"Id": "a7g6s0000004GZzAAM",
"Number": "00096412.0",
"Name": "ISOLIN"
}
]
}
Expecting output JSON :
{
"Size" : "2",
"done" : "true",
"Items" : [ {
"Fields" : [ {
"Name" : "Id",
"Value" : "a7g6s0000004GZuAAM"
}, {
"Name" : "NN",
"Value" : "00096411.0"
}, {
"Name" : "Name",
"Value" : "ISOLIN TRADE & INVEST"
}, {
"Name" : "RecordType_Name",
"Value" : "Term"
} ]
}, {
"Fields" : [ {
"Name" : "Id",
"Value" : "a7g6s0000004GZzAAM"
}, {
"Name" : "Number",
"Value" : "00096412.0"
}, {
"Name" : "Name",
"Value" : "ISOLIN"
} ]
} ]
}
I am using below jolt spec but transformation of RecordType element is not as expected :
Jolt Spec :
[
{
"operation": "remove",
"spec": {
"records": {
"*": {
"attributes": " "
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"records": {
"*": {
"*": {
"$": "Items.&2.[#2].Name",
"#": "Items.&2.[#2].Value"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"Items": {
"*": {
"*": "Items.[#2].Fields[]"
}
}
}
}
]
How can I transform this into required format ?
I think remove transformation is not needed, and better to index each Field.
The trick for the solution would be distinguish RecordType attribute from the others by use of
"RecordType": {"Name": "Field&2.&1\\_&"}
such as
[
{
"operation": "shift",
"spec": {
"*": "&",
"records": {
"*": {
"*": "Field&1.&",
"RecordType": {
"Name": "Field&2.&1\\_&"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"Field*": {
"*": {
"$": "Items[0].&2.[#2].Name",
"#": "Items[0].&2.[#2].Value"
}
}
}
}
]

Jolt transform json array

I need a help in Jolt transform.
I have array, that consist from many json objects. (https://jolt-demo.appspot.com/ )
[
{
"table_name": "table_vd",
"scratch_name": "l1_sample_1_1",
"scratch_order": 1
},
{
"table_name": "table_vd",
"scratch_name": "l1_sample_1_1",
"scratch_order": 34
},
{
"table_name": "table_vd",
"scratch_name": "l1_sample_2",
"scratch_order": 3
}
]
What transformation of Jolt needs to be done to get the following result?
{
"table_name" : "table_vd",
"data" : [ {
"scratch_name" : "l1_sample_1_1",
"scratch_order" : 1
}, {
"scratch_name" : "l1_sample_1_1",
"scratch_order" : 34
}, {
"scratch_name" : "l1_sample_2",
"scratch_order" : 3
} ]
}
Now i have next Jolt construction:
[
{
"operation": "shift",
"spec": {
"*": {
"table_name": "table_name",
"*": {
"#0": "data[0].&1"
}
}
}
}
]
But the result below is not correct:
{
"table_name" : [ "table_vd", "table_vd", "table_vd" ],
"data" : [ {
"scratch_name" : [ "l1_sample_1_1", "l1_sample_1_1", "l1_sample_2" ],
"scratch_order" : [ 1, 34, 3 ]
} ]
}
You started well by using conditional logic through seperating "table_name" and others("*"). Rearrange "*"(the rest) as "*": "data[&1].&", and then pick the first element of the values of "table_name" array through use of cardinality spec such as
[
{
"operation": "shift",
"spec": {
"*": {
"table_name": "&",
"*": "data[&1].&"
}
}
},
{
"operation": "cardinality",
"spec": {
"table_name": "ONE"
}
}
]

JOLT spec array of objects remove key

I'm trying to understand how to remove the key and object layers for the below. Any help would be greatly appreciated. Trying to use a JOLT processor in NIFI to address this data change.
INPUT:
[ {
"rbr" : {
"fetchTime" : "2020-07-06T23:46:23.677Z",
"customMetadata" : {
"x" : "1",
"o2" : {
"x2" : "y"
}
}
},
"xyz": {
"fetchTime" : "2020-07-06T23:46:23.677Z",
"customMetadata" : {
"x" : "1",
"o2" : {
"x2" : "y"
}
}
}
}
]
desired output:
[
{
"fetchTime" : "2020-07-06T23:46:23.677Z",
"customMetadata" : {
"x" : "1",
"o2" : {
"x2" : "y"
}
},
"type": "rbr"
},
{
"fetchTime" : "2020-07-06T23:46:23.677Z",
"customMetadata" : {
"x" : "1",
"o2" : {
"x2" : "y"
}
},
"type": "xyz"
}
]
Can be done with simple shift operation
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"fetchTime": "[#2].fetchTime",
"customMetadata": "[#2].customMetadata",
"$": "[#2].type"
}
}
}
}
]
Edit 1
Can shift all the values from current level using "#": "[#3].&",
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"#": "[#3].&",
"$1": "[#3].type"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"type": "=firstElement(#(1,type))"
}
}
}
]
Iterate over the array, and for each obj create newObj like so:
objKey=Object.keys(obj)[0];
newObj=obj[objKey];
newObj.type=objKey;
and push into new array.

Jolt, copy value along array

I want to transform a JSON using JOLT, in the following way.:
Input of json file
{
"Document_No": "ANG4",
"LineNo10": {
"Type": 1,
"Customer_No_": "1"
},
"LineNo11": {
"Type": 2,
"Customer_No_": "2"
},
"LineNo12": {
"Type": 3,
"Customer_No_": "3"
}
}
Desired output of json file
[
{
"Document_No":"ANG4"
"Type" : 1,
"Customer_No_" : "1",
"Line_No" : "10"
},
{
"Document_No":"ANG4"
"Type" : 2,
"Customer_No_" : "2",
"Line_No" : "11"
},
{
"Document_No":"ANG4"
"Type" : 3,
"Customer_No_" : "3",
"Line_No" : "12"
}
]
I don't lack a lot, but i don't know what transformations add to get the desired output
my Jolt specificaton is below
[
{
"operation": "shift",
"spec": {
"LineNo*": {
"#": "&",
"$": "&.Line_No"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"#": "[]"
}
}
}
]
My output json file :
[ {
"Type" : 1,
"Customer_No_" : "1",
"Line_No" : "LineNo10"
}, {
"Type" : 2,
"Customer_No_" : "2",
"Line_No" : "LineNo11"
}, {
"Type" : 3,
"Customer_No_" : "3",
"Line_No" : "LineNo12"
} ]
Could anyone help me with this?
Spec
[
{
"operation": "shift",
"spec": {
"LineNo*": {
// copy Document_No down into the "LineNo" maps
"#(1,Document_No)": "&1.Document_No",
"$": "&1.Line_No",
"*": "&1.&"
}
}
},
{
// accumulate all the "built" LineNo's into an array
"operation": "shift",
"spec": {
"*": {
"#": "[]"
}
}
}
]