JOLT Show If else - json

I have a problem and I don't know how to solve it, I want to get data from "B" if it exists and if it doesn't exist then data from "A".
I've done it but it only works for B, could it be improved in my code?
Input with B:
`
{
"P": {
"A": {
"One": {
"Two": {
"Three": [
{
"ID": "123456",
"Some": {
"ID_1": "123456789",
"AAA": "aaa"
}
}
]
}
}
},
"B": {
"One": {
"Two": {
"Three": [
{
"ID": "654321",
"Some": {
"ID_1": "987654321",
"BBB": "bbb"
}
}
]
}
}
}
}
}
`
Input without B:
`
{
"P": {
"A": {
"One": {
"Two": {
"Three": [
{
"ID": "123456",
"Some": {
"ID_1": "123456789",
"AAA": "aaa"
}
}
]
}
}
}
}
}
`
JSON Spec:
`
[
{
"operation": "modify-default-beta",
"spec": {
"P": {
"B": {
"One": {
"Two": {
"Three": {
"*": {
"ID": "null"
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"P": {
"B": {
"One": {
"Two": {
"Three": {
"*": {
"ID": {
"null": {
"#(6,A.One.Two.Three.ID)": "Data.ID",
"#(6,A.One.Two.Three.Some.ID_1)": "Data.ID_1",
"#(6,A.One.Two.Three.Some.AAA)": "Data.AAA"
},
"*": {
"#(2,ID)": "Data.ID",
"#(2,Some.ID_1)": "Data.ID_1",
"#(2,Some.BBB)": "Data.BBB"
}
}
}
}
}
}
}
}
}
}
]
`
Output:
{
"Data" : {
"ID" : "654321",
"ID_1" : "987654321",
"BBB" : "bbb"
}
}

Related

JOLT merge two arrays with common values

I have a JSON object like this:
{
"data":{
"list1":[
{
"id":"1",
"description":"abc..",
"title":"abc"
},
{
"id":"2",
"description":"hello..",
"title":"hello"
},
{
"id":"3",
"description":"hello..",
"title":"hello"
},
{
"id":"4",
"description":"hello..",
"title":"hello"
},
{
"id":"5",
"description":"hello..",
"title":"hello"
},
{
"id":"6",
"description":"hello..",
"title":"hello"
}
],
"list2":[
{
"info":"this is list2",
"idList":[
"1",
"3"
]
},
{
"info":"this is list2",
"idList":[
"2",
"4"
]
}
]
}
}
In the transformed JSON I want everything of list1 inside idList of list2 whenever the id matches. Like this:
{
"res":{
"data":[
{
"info":"this is list2",
"idList":[
{
"id":"1",
"description":"abc..",
"title":"abc"
},
{
"id":"3",
"description":"hello..",
"title":"hello"
}
]
},
{
"info":"this is list2",
"idList":[
{
"id":"2",
"description":"hello..",
"title":"hello"
},
{
"id":"4",
"description":"hello..",
"title":"hello"
}
]
}
]
}
}
How can I use Jolt Spec to achieve this transformation?
[
{
"operation": "shift",
"spec": {
"data": {
"list1": {
"*": {
"id": {
"#(3,list2[&].idList)": {
"#(4,list2[&].idList)": "res.data[&3].idList"
}
}
}
},
"list2": {
"*": {
"info": "res.data[&1].info"
}
}
}
}
}
]
I tried this spec to match those two lists however I don't get anything for the list1 spec. The result I get is only the info part:
{
"res" : {
"data" : [ {
"info" : "this is list2"
}, {
"info" : "this is list2"
} ]
}
}
You can use this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"list1": {
"*": {
"*": "&3.&2.#(1,id).&"
}
},
"*": "&1.&"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"list2": {
"*": {
"*": "res.&3[&1].&",
"idList": {
"*": {
"*": {
"#(5,list1.&)": "res.&6.[&4].&3"
}
}
}
}
}
}
}
}
]

How to use same field value at multiple places in Jolt

I have a requirement for transforming JSON and I am trying to use the same value multiple times. Is there a way to use value multiple times previously I did use in array but this time I have to go through the level. Any help is appreciated and thank you.
Note I want to filter product configurations based on the name.
How to use same field value at multiple places in Jolt
Input:
{
"sample": {
"Result": {
"value": {
"ImplProductConfigurationsOptionsEcomResponse": {
"transactionId": "12345678",
"timeStamp": 1646087177771,
"orderCorrelationId": "11039714",
"implValidateMultiProductConfigurationsResponse": {
"productConfigurations": [
{
"productConfiguration": {
"productSpecification": {
"name": "Private",
"id": "247541"
}
}
},
{
"productConfiguration": {
"productSpecification": {
"name": " sample Miscellaneous",
"id": "22222"
}
}
}
]
}
}
}
}
}
}
Current output:
{
"productConfigurations": {
"productConfiguration": {
"productSpecification": {
"name": "Private",
"id": "247541"
}
}
}
}
Expected Output:
{
"productConfigurations": {
"productConfiguration": {
"productSpecification": {
"name": "Private",
"id": "247541"
}
}
},
"Details": {
"ImplProductConfigurationsOptionsEcomResponse": {
"transactionId": "12345678",
"timeStamp": 1646087177771,
"orderCorrelationId": "11039714",
"implValidateMultiProductConfigurationsResponse": {
"productConfigurations": [
{
"productConfiguration": {
"productSpecification": {
"name": "Private",
"id": "247541"
}
}
},
{
"productConfiguration": {
"productSpecification": {
"name": " sample Miscellaneous",
"id": "22222"
}
}
}
]
}
}
}
}
My Spec:
[
{
"operation": "shift",
"spec": {
"sample": {
"Result": {
"value": {
"ImplProductConfigurationsOptionsEcomResponse": {
"implValidateMultiProductConfigurationsResponse": {
"productConfigurations": {
"*": {
"productConfiguration": {
"productSpecification": {
"name": {
"*Miscellaneous": null,
"*": {
"#4": "productConfigurations"
}
}
}
}
}
}
}
}
}
}
}
}
}
]
You can reference the object twice along with a shift transformation spec such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": {
"*": "Details.&1.&",
"implValidateMultiProductConfigurationsResponse": {
"*": {
"*": {
"*": {
"*": {
"name": {
"*Miscellaneous": {
"#3": "Details.&8.&7.&6[].&4"
},
"*": {
"#4": "&6",
"#3": "Details.&8.&7.&6[].&4"
}
}
}
}
}
}
}
}
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/

Jolt specification to transform JSON array within array

I'm trying to transform a JSON file into something that can later be transformed to csv or xml. My JSON file looks like this:
{
"meta": {
"contentType": "Response"
},
"content": {
"_type": "Response",
"data": {
"_type": "ObjectList",
"meta": {
"_type": "ObjectListMeta"
},
"DataObjects": [
{
"head": {
"fields": {
"id": {
"value": "24"
},
"name": {
"value": ""
}
}
},
"table": [
{
"number": 1,
"fields": {
"height": {
"value": 500,
"text": "500"
},
"average": {
"value": -2,
"text": "-2"
}
}
},
{
"number": 2,
"fields": {
"height": {
"value": 99999,
"text": "99999"
},
"average": {
"value": -5,
"text": "-5"
}
}
}
]
}
]
}
}
}
I want the output to look like this:
[
{
"id": "24",
"name": "",
"height": 500,
"average": -2
},
{
"id": "24",
"name": "",
"height": 99999,
"average": -5
}
]
I tried the following JOLT specification:
[
{
"operation": "shift",
"spec": {
"content": {
"data": {
"DataObjects": {
"*": {
"head": {
"fields": {
"id": {
"value": "[&4].id"
},
"name": {
"value": "[&4].name"
}
}
},
"table": {
"*": {
"fields": {
"height": {
"value": "[&5].height"
},
"average": {
"value": "[&5].average"
}
}
}
}
}
}
}
}
}
}
]
But I only get the following output:
[
{
"id": "24",
"name": "",
"height" : [ 500, 99999 ],
"average" : [ -2, -5 ]
}
]
Which is not exactly what I want. How do I have to modify my spec to get the output I need?
You can combine those attributes under the table list as having multiple respective objects for one head.fields object such as
[
{
"operation": "shift",
"spec": {
"content": {
"*": {
"DataObjects": {
"*": {
"table": {
"*": {
"#(2,head.fields.id.value)": "[&].id",
"#(2,head.fields.name.value)": "[&].name",
"#(0,fields.height.value)": "[&].height",
"#(0,fields.average.value)": "[&].average"
}
}
}
}
}
}
}
}
]

Jolt specification needed for the below input and expected output

I need to split the data list into two separate idExists and idNotExists lists, based on whether id exists or not.
Can someone help me with Jolt Specification to achieve the below results?
Input:
{
"data": [
{
"name": "a",
"id": "100"
},
{
"name": "b",
"id": "101"
},
{
"name": "c"
}
]
}
Desired Output:
{
"IdExists": [
{
"name": "a",
"id": "100"
},
{
"name": "b",
"id": "101"
}
],
"IdNotExists": [
{
"name": "c"
}
]
}
I have tried with the below spec but it is not working as expected
[
{
"operation": "modify-default-beta",
"spec": {
"data": {
"": {
"id": "NotExist"
}
}
}
},
{
"operation": "shift",
"spec": {
"data": {
"": {
"id": {
"10*": {
"#2": "IdExists[]"
},
"*": {
"#2": "IdNotExists[]"
}
}
}
}
}
}
]
I had some modification on your spec, added * as it was missing in the selectors.
Modified shifting condition to NotExist and finally shift only name to the IdNotExists array.
[
{
"operation": "modify-default-beta",
"spec": {
"data": {
"*": {
"id": "NotExist"
}
}
}
},
{
"operation": "shift",
"spec": {
"data": {
"*": {
"id": {
"NotExist": {
"#(2,name)": "IdNotExists[].name"
},
"*": {
"#2": "IdExists[]"
}
}
}
}
}
}
]

Output multiple duplicate keys instead of array in Jolt spec

I need to rearrange incoming structure and output multiple duplicate keys with different values, but jolt spec is giving me array instead of keys
Input data:
{
"TestData": {
"data": {
"Products": [
{
"Name": {
"key": "Name__c",
"value": "Example1"
}
},
{
"Name": {
"key": "Name__c",
"value": "Example2"
}
}
]
}
}
}
Desired output:
{
"TestData": {
"data": {
"Name__c": "Example1",
"Name__c": "Example2"
}
}
}
When i'm using following spec:
[
{
"operation": "shift",
"spec": {
"*": {
"data": {
"Products": {
"*": {
"Name": {
"value": "TestData.data.#(1,key)"
}
}
}
}
}
}
}
]
Its giving me
{
"TestData": {
"data": {
"Name__c": [
"Example1",
"Example2"
]
}
}
}
Does anyone know how to handle this in Jolt?
Thanks