I am news to jolt json transformation. So looking for help in transforming nested json having list of values to nested json with list as following.
Input json
{
"userData": [
{
"userId": "1",
"age": "20",
"firstName": "firstname1",
"lastname": "lastname1",
"zipCode": "zipcode1",
"street": "street1",
"city": "city1",
"country": "country",
"gender": "gender1",
"grade": "grade1",
"birthday": "birthday1"
},
{
"userId": "2",
"age": "25",
"firstName": "firstname2",
"lastname": "lastname2",
"zipCode": "zipcode2",
"street": "street2",
"city": "city2",
"country": "country2",
"gender": "gender2",
"grade": "grade2",
"birthday": "birthday2"
}
]
}
Jolt Specification
[
{
"operation": "shift",
"spec": {
"userData": {
"*": {
"userId": "data.[&1].ID",
"*": "data.[&1].&",
"zipCode": {
"#custom-field1": "adr_&1.property",
"@": "adr_&.value"
},
"street": {
"#custom-field2": "adr_&1.property",
"@": "adr_&.value"
},
"city": {
"#custom-field3": "adr_&1.property",
"@": "adr_&.value"
},
"country": {
"#custom-field4": "adr_&1.property",
"@": "adr_&.value"
}
}
}
}
}
]
Output
{
"data" : [ {
"ID" : "1",
"age" : "20",
"firstName" : "firstname1",
"lastname" : "lastname1",
"gender" : "gender1",
"grade" : "grade1",
"birthday" : "birthday1"
}, {
"ID" : "2",
"age" : "25",
"firstName" : "firstname2",
"lastname" : "lastname2",
"gender" : "gender2",
"grade" : "grade2",
"birthday" : "birthday2"
} ],
"adr_zipCode" : {
"property" : [ "custom-field1", "custom-field1" ],
"value" : [ "zipcode1", "zipcode2" ]
},
"adr_street" : {
"property" : [ "custom-field2", "custom-field2" ],
"value" : [ "street1", "street2" ]
},
"adr_city" : {
"property" : [ "custom-field3", "custom-field3" ],
"value" : [ "city1", "city2" ]
},
"adr_country" : {
"property" : [ "custom-field4", "custom-field4" ],
"value" : [ "country", "country2" ]
}
}
Expected json
{
"data": [
{
"ID": "1",
"age": "20",
"firstName": "firstname1",
"lastname": "lastname1",
"gender": "gender1",
"grade": "grade1",
"birthday": "birthday1",
"address": [
{
"property": "custom-field1",
"value": "zipcode1"
},
{
"property": "custom-field2",
"value": "street1"
},
{
"property": "custom-field3",
"value": "city1"
},
{
"property": "custom-field4",
"value": "country"
}
]
},
{
"ID": "2",
"age": "25",
"firstName": "firstname2",
"lastname": "lastname2",
"gender": "gender2",
"grade": "grade2",
"birthday": "birthday2",
"address": [
{
"property": "custom-field1",
"value": "zipcode2"
},
{
"property": "custom-field2",
"value": "street2"
},
{
"property": "custom-field3",
"value": "city2"
},
{
"property": "custom-field4",
"value": "country2"
}
]
}
]
}
3
Answers
You can use individual
value
arrays of their indexes to be used within the upcoming specs with a tricky zeroth member which will be removed later, after been used such asYou may consider another library Josson to do the same job by shorter transformation statement.
https://github.com/octomix/josson
Deserialization
Transformation
Output