How to get object with certain property in arraylist in Jolt - arrays

I want to do a Jolt transformation with a json array, and i only want the ones with certain attribute.
For example:
Input:
{
"characteristic": [
{
"name": "BrandId",
"value": "b"
},
{
"name": "status",
"value": "SENT"
},
{
"name": "statusTxt",
"value": "sent"
}
]
}
I want output to be
{
"status":"SENT",
"statusTxt":"sent"
}

This will lead to the output you want based on your input:
[
{
"operation": "shift",
"spec": {
"characteristic": {
"*": {
"name": {
"status": {
"#(2,value)": "status"
},
"statusTxt": {
"#(2,value)": "statusTxt"
}
}
}
}
}
}
]

Related

JOLT : Make Nested Make a nested array as part of main array

I am trying to transform the JSON as below expected output. Stuck with the spec below. Can someone help in this?
There is an inner array with name "content" in the "results" array which I want to make it as a part of main array.
Input JSON
{
"total": 100,
"start": 1,
"page-length": 10,
"results": [
{
"index": 1,
"uri": "uri1/uri2",
"extracted": {
"kind": "object",
"content": [
{
"code": "A1",
"region": "APAC"
}
]
}
},
{
"index": 2,
"uri": "uri1/uri2",
"extracted": {
"kind": "object",
"content": [
{
"code": "B1",
"region": "AMER"
}
]
}
},
{
"index": 3,
"uri": "uri1/uri2",
"extracted": {
"kind": "object",
"content": [
{
"code": "C1",
"region": "APAC"
}
]
}
}
]
}
Expected json output
[
{
"code": "A1",
"region": "APAC"
},
{
"code": "B1",
"region": "AMER"
},
{
"code": "C1",
"region": "APAC"
}
]
Spec Tried
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"extracted": { "content": { "#": "&" } }
}
}
}
},
{
"operation": "shift",
"spec": {
"content": {
"#": "&"
}
}
}
]
Find the output below I am getting on Jolt tool
You can use # wildcard nested within square brackets in order to reach the level of the indexes of the "results" array such that
[
{
"operation": "shift",
"spec": {
"results": {
"*": {//the indexes of the "results" array
"extracted": {
"content": {
"*": {//the indexes of the "content" array
"*": "[#5].&"
}
}
}
}
}
}
}
]
Also the following spec, which repeats the content of the inner array without keys, will give the same result :
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"extracted": {
"content": {
"*": "[]"// [] seems like redundant but kept for the case the array has a single object.
}
}
}
}
}
}
]
which is quite similar to your tried one.

Jolt transformation needed for multidimensional array

I am trying to transform the data from a multi array to single array using jolt technique. But unable to do so. Below are the details.
Input file:
{
"oi": [
{
"ei": [
{
"type": "bs",
"id": "797416713"
}
]
},
{
"ei": [
{
"type": "bs",
"id": "797416716"
}
]
}
]
}
Jolt file used is as below:
[
{
"operation": "shift",
"spec": {
"oi": {
"*": {
"ei": {
"*": {
"type": {
"bs": {
"#(2,id)": "oi[#3].bs"
}
}
}
}
}
}
}
}
]
Expected output from above is as below.
{
"oi": [
{
"bs": [
"797416713",
"797416716"
]
}
]
}
Actual output coming from jolt is :
{
"oi": [
{
"bs": [
"797416713",
"797416716"
]
}
]
}
Currently the desired and actual output values are identical.
I understand from the expression
to transform the data from a multi array to single array
that you need to get such a result of type array of objects tagged current inner(ei) and outer(oi) keys such as(if not please clearly state) :
[
{
"oi": {
"ei": [
{
"type": "bs",
"id": "797416713"
},
{
"type": "bs",
"id": "797416716"
}
]
}
}
]
Then, you can use this shift transformation spec :
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": {
"#": "[#2].&4.&2"
}
}
}
}
}
}
]
where &4 will grab the key oi after going 4 levels up, &2 will grab the key ei after going 2 levels up the tree

Unable to properly Transform nested array with JOLT

I'm trying to convert json nested array's element and unable to get expected result, either I properly get name element or schemaExtensions element but can't get both together.
Here is my input:
{
"rows": [
{
"content": {
"name": {
"content": "User"
},
"schemaExtensions": {
"content": [
{
"content": {
"schema": {
"content": "User"
},
"required": {
"content": true
}
}
}
]
}
}
}
]
}
And the JOlt spec definition:
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"content": {
"name": {
"content": "[&3].name"
},
"schemaExtensions": {
"content": {
"*": {
"content": {
"schema": {
"content": "schemaExtensions.[&7].schema"
},
"required": {
"content": "schemaExtensions.[&7].required"
}
}
}
}
}
}
}
}
}
}
]
The expected result:
[ {
"name" : "User",
"schemaExtensions": [ {
"schema":"User",
"required":true
}]
}]
Here is the result I got when having both in my spec
[ {
"name" : "User"
} ]
And if I remove name from my spec, then I get my schemaExtensions properly:
{
"schemaExtensions" : [ {
"schema" : "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
"required" : true
} ]
}
You can start with three level nesting to roam before writing the branches(name&schemaExtensions) explicitly. No need to write other key names such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"name": { "*": "&1" },
"schemaExtensions": {
"*": {
"*": { "*": { "*": { "*": "&5.&1" } } }
}
}
}
}
}
}
}
]
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"content": {
"name": {
"content": "[].name"
},
"schemaExtensions": {
"content": {
"*": {
"content": {
"schema": {
"content": "[&7].schemaExtensions[&7].schema"
},
"required": {
"content": "[&7].schemaExtensions[&7].required"
}
}
}
}
}
}
}
}
}
}
]

JOLT array transformation: add key in all objects in list

I have an input object:
{
"array": [
{
"id": 1
},
{
"id": 2
}
],
"object": {
"fixed-value": "some-value"
}
}
And I want to transform it into:
{
"NewObject" : [ {
"objectId" : 1,
"fixedValue": "some-value"
}, {
"objectId" : 2,
"fixedValue": "some-value"
} ]
}
I made this JOLT spec which shifts the list of objects in array, but I'm not able to add the fixed-value key in all of these objects:
[
{
"operation": "shift",
"spec": {
"array": {
"*": {
"id": "NewObject[&1].objectId"
}
}
}
}
]
Check this spec,
Traverse back to the root and then select the fixed-value,
"#(2,object.fixed-value)": "NewObject[&1].fixedValue"
[
{
"operation": "shift",
"spec": {
"array": {
"*": {
"id": "NewObject[&1].objectId",
"#(2,object.fixed-value)": "NewObject[&1].fixedValue"
}
}
}
}
]

parsing the array of objects in JSON and converting it to flat JSON using JOLT transform

My input looks like the below;
{
"family": [
{
"person": {
"personId": {
"value": "12345"
},
"employeeAuthCd": {
"code": "AUTH_12345"
},
"employeeTypeCd": {
"code": "cd"
},
"status": {
"code": "New"
}
}
}
]
}
Desired Output
{
"Person_ID":"12345",
"employeeAuthCd":"AUTH_1345",
"employeeTypeCd":"cd",
"status":"New"
}
Can anyone help me out with the Jolt spec, I have tried many possible specs but couldn't reach the desired output, like the above, JSON have multiple array of objects those I need to convert those into flat JSON
This spec should work for you:
[
{
"operation": "shift",
"spec": {
"family": {
"*": {
"person": {
"personId": {
"value": "Person_ID"
},
"employeeAuthCd": {
"code": "employeeAuthCd"
},
"employeeTypeCd": {
"code": "employeeTypeCd"
},
"status": {
"code": "status"
}
}
}
}
}
}
]

Resources