How to convert string "{}" to empty dictionary {} JSON in Jolt? I am trying to replace "{}" by {} in Jolt - json

Input JSON value :
{
"age": [
{
"r": "r1",
"d": "{}"
}
]
}
Desired Output :
{
"age": [
{
"r": "r1",
"d": {}
}
]
}
I tried using modify-overwrite-beta but unable to replace string by empty dictionary.

You can use the following shift transformation spec in order to convert {} to null, and then modify transformation spec along with notNull function to return the desired result such as
[
{
"operation": "shift",
"spec": {
"age": {
"*": {
"*": {
"*": { "#1": "&4[&3].&2" },
"{}": "&3[&2].&1"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"age": {
"*": {
"*": ["=notNull", {}]
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

You can use this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&2.[&1].&",
"d": {
"{}": "&3.[&2].&1.temp"
}
}
}
}
},
{
"operation": "remove",
"spec": {
"age": {
"*": {
"d": {
"*": ""
}
}
}
}
}
]

Related

JOLT Specification to Transform JSON field value to field name

I am trying to convert the below JSON payload into a JSON which has the field name as the value of the field:
The jolt file that I have is working if field values are different. But if field values are the same then it is giving an array in the response.
Can you please help to provide jolt specifications for this?
Input JSON Payload :
{
"action": {
"allowPartialSuccess": true,
"records": [
{
"ZuoraSubscriptionExtId": "962041ad-e673-492a-a071-5e0ab74ea001",
"CPQSubscriptionID__c": "5640029343"
},
{
"ZuoraSubscriptionExtId": "962041ad-e673-492a-a071-5e0ab74ea001",
"CPQSubscriptionID__c": "5640029343"
}
],
"type": "update"
}
}
`
Expected output JSON Payload :
{
"action" : {
"allowPartialSuccess" : true,
"records" : {
"962041ad-e673-492a-a071-5e0ab74ea001" : {
"CPQSubscriptionID__c" : "5640029343"
}
}, {
"962041ad-e673-492a-a071-5e0ab74ea001" : {
"CPQSubscriptionID__c" : "5640029343"
}
} ,
"type" : "update"
}
}
JOLT specification that I am using :
[
{
"operation": "shift",
"spec": {
"action": {
"allowPartialSuccess": "action.allowPartialSuccess",
"records": {
"*": {
"CPQSubscriptionID__c": "action.records.#(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
}
},
"type": "action.type"
}
}
}
]
You can use this shift transformation spec
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&", // &1 replicates the literal "action"
"records": {
"*": {
"C*": "&3.&2.#(1,ZuoraSubscriptionExtId).&" // &3 replicates the literal "action" (by going three level up the tree), &2 for "records", & replicates the current level attribute
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
Your expected output is wrong in the question.
If you want an object in your record, You can use this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"&": "&1.&",
"records": {
"*": {
"CPQSubscriptionID__c": "&3.&2.#(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
}
}
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"records": {
"*": {
"*": "ONE"
}
}
}
}
}
]
And if you want an array in your output. You can use this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&",
"records": {
"*": {
"CPQSubscriptionID__c": "&3.&2[].#(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
}
}
}
}
}
]

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 moving from LHS to RHS and flattening

I am trying to convert the following JSON with JOLT but struggling, any help would be appreciated.
{
"2021-04-14T00:00:00+02:00": {
"249184": {
"SRAD": null,
"T": -50.00000000000001
},
"249185": {
"SRAD": 0.46133333444595337,
"T": null
}
},
"2021-04-14T00:15:00+02:00": {
"249184": {
"SRAD": null,
"T": -50.00000000000001
},
"249185": {
"SRAD": 0.4593333303928375,
"T": null
}
}
}
Desired output: Note here that the timestamps are repeated for each deviceID along with the SRAD and T values.
{
"timestamp": "2021-04-14T00:00:00+02:00",
"deviceID": 249184,
"SRAD":null,
"T":-50.00000000000001
},
{
"timestamp": "2021-04-14T00:00:00+02:00",
"deviceID": 249185,
"SRAD":0.46133333444595337,
"T":null
},
{
"timestamp": "2021-04-14T00:15:00+02:00",
"deviceID": 249184,
"SRAD":null,
"T":-50.00000000000001
},
{
"timestamp": "2021-04-14T00:15:00+02:00",
"deviceID": 249185,
"SRAD": 0.4593333303928375,
"T":null
}
I have tried a bunch of things but keep going in circles.
Here's an alternate spec with 2 shifts that supports any number of fields inside the deviceId object:
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"$(1)": "a[].timestamp",
"$": "b[].deviceId",
"#": "c[]"
}
}
}
},
{
"operation": "shift",
"spec": {
"a": {
"*": {
"timestamp": "[&1].timestamp",
"#(2,b[&])": "[&1]"
}
},
"b": null,
"c": {
"*": {
"*": "[&1].&"
}
}
}
}
]
[
// Change null as String, as jolt will not
// process null values
{
"operation": "modify-default-beta",
"spec": {
"*": {
"*": {
"SRAD": "null",
"T": "null"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"SRAD": "&4.SRAD",
"T": "&4.T",
"$0": "&4.deviceID",
"$1": "&4.timestamp"
}
}
}
}, {
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"#": "&"
}
}
}
}
}, {
"operation": "shift",
"spec": {
"*": {
"0": "[&1].deviceID",
"1": "[&1].timestamp",
"2": "[&1].SRAD",
"3": "[&1].T"
}
}
}
]
You cannot process the JSON having null as value, so null is replaced with "null" using modify-default-beta operation.
Input JSON is grouped by timestamp and deviceid, so deconstruct the json by assigning names to the children nodes and then construct the output JSON.

How to remove parent and modify input JSON fields using JOLT specification?

I've to remove _id field, prefix date with $date and remove JSON from the input JSON. Can anyone help to achieve this using JOLT spec?
INPUT:
{
"JSON":{
"_id":{
"oid":"5f9122213f077e24b639d084"
},
"name":"Mongodb",
"age":"98",
"ttlTime":{
"date":1536165487000
}
}
}
Expected Output:
{
"name":"Mongodb",
"age":"98",
"ttlTime":{
"$date":1536165487000
}
}
I'm using the below spec, but not getting the desired output.
JOLT spec:
[
{
"operation":"remove",
"spec":{
"JSON":{
"_id":""
}
}
},
{
"operation":"shift",
"spec":{
"JSON":{
"ttlTime":{
"date":"ttlTime.\\$date"
}
},
"*":"&",
"JSON":""
}
}
]
check this spec,
[
{
"operation": "shift",
"spec": {
"JSON": {
"name": "name",
"age": "age",
"ttlTime": {
"date": "ttlTime.\\$date"
}
}
}
}
]
You can using this spec:
[
{
"operation": "shift",
"spec": {
"JSON": {
"_*": {
"*": {
"*": ""
}
},
"*": "&",
"ttlTime": {
"*": "ttlTime.\\$date"
}
}
}
}
]

Json array elements duplication using jolt

I want to know if jolt can handle the following transformation:
{
"interface": [
{
"field": "A",
"ip": [
"1.1.1.1",
"1.1.1.2"
]
},
{
"field": "B",
"ip": [
"1.1.1.3"
]
}
]
}
to
{
"interface": [
{
"field": "A",
"ip": "1.1.1.1"
},
{
"field": "A",
"ip": "1.1.1.2"
},
{
"field": "B",
"ip": "1.1.1.3"
}
]
}
i.e for a JSON array containing a child array, create one version of parent item for each of its child array item.
Can jolt do that?
This can be achieved by doing a two stage shift:
Create an array of arrays
Then flatten the array of arrays
[
{
"operation": "shift",
"spec": {
"interface": {
"*": {
"ip": {
"*": {
"#": "[&3].[&1].ip",
"#(2,field)": "[&3].[&1].field"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "interface.[]"
}
}
}
]
It is not complete answer, but maybe that helps you somehow or someone else could help?
[
{
"operation": "shift",
"spec": {
"interface": {
"*": {
"ip": {
"*": {
"#": "ip",
"#(2,field)": "field"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"ip": {
"*": {
"#": "interface[&1].ip",
"#(2,field)": {
"???": "interface[&2].field" // How to take proper index of an array?
}
}
}
}
}
]