If id value is 0 then i don't want that in output. Photos Array is dynamic in nature. Here it is 3 , it can be 3 or 4 or5 any . Your help in this is highly appreciated . thanks .
input JSON
{
"Entity": {
"card": {
"cardNo": "123456789",
"cardStatus": "10",
"cardAddress": "UK",
"cardAddress1": "US",
"cardCity": "mk",
"name": "RAM",
"lastName": "ABU",
"name1": "RAM1",
"lastName1": "ABU1",
"Id1": "0",
"Id2": 0,
"Id3": 0
},
"Photos": [
{
"Id": 327703,
"Caption": "TEST>> photo 1",
"Url": "http://bob.com/0001/327703/photo.jpg"
},
{
"Id": 327704,
"Caption": "TEST>> photo 2",
"Url": "http://bob.com/0001/327704/photo.jpg"
},
{
"Id": 327704,
"Caption": "TEST>> photo 2",
"Url": "http://bob.com/0001/327704/photo.jpg"
}
]
}
}
output JSON
{
"tab": {
"text": "123456789"
},
"address": [
{
"add": "UK",
"add2": "US",
"mk": "mk"
}
],
"Photos": [
{
"caption2": "http.1.com",
"no": "0"
},
{
"caption2": "http.2.com",
"no": "0"
},
{
"caption2": "http.3com",
"no": "0"
},
{
"caption2": "TEST>> photo 1",
"no": "327703"
},
{
"caption2": "TEST>> photo 2",
"no": "327704"
},
{
"caption2": "TEST>> photo 2",
"no": "327704"
}
]
}
expected JSON
{
"tab": {
"text": "123456789"
},
"address": [
{
"add": "UK",
"add2": "US",
"mk": "mk"
}
],
"Photos": [
{
"caption2": "TEST>> photo 1",
"no": "327703"
},
{
"caption2": "TEST>> photo 2",
"no": "327704"
},
{
"caption2": "TEST>> photo 2",
"no": "327704"
}
]
}
jolt spec using
[
{
"operation": "shift",
"spec": {
"Entity": {
"card": {
"cardNo": "tab.text",
"cardAddress": "address[0].add",
"cardAddress1": "address[0].add2",
"cardC*": "address[0].mk",
"Id1": "Photos.no",
"#http.1.com": "Photos.caption2",
"Id2": "Photos.no",
"#http.2.com": "Photos.caption2",
"Id3": "Photos.no",
"#http.3com": "Photos.caption2"
},
"Photos": {
"*": {
"Id": "Photos.no",
"Caption": "Photos.caption2"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"tab": "&",
"address": "&",
"Photos": {
"*": {
"*": {
"@": "&3[&1].&2"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"Photos": {
"*": {
"no": "=toString"
}
}
}
}
]
I am thinking one if else logic is required if value is 0 then don't pass otherwise pass like that.
CodePudding user response:
Hey Saurabh I couldn't understand your request properly but this may help you resolve your query. I have created an spec for your expected output, I have kept an if else condition on Photos[].no if its 0 it wont populate the fields, you can check the below spec and input :
Input :
{
"Entity": {
"card": {
"cardNo": "123456789",
"cardStatus": "10",
"cardAddress": "UK",
"cardAddress1": "US",
"cardCity": "mk",
"name": "RAM",
"lastName": "ABU",
"name1": "RAM1",
"lastName1": "ABU1",
"Id1": "0",
"Id2": 0,
"Id3": 0
},
"Photos": [
{
"Id": 0,
"Caption": "TEST>> photo 1",
"Url": "http://bob.com/0001/327703/photo.jpg"
},
{
"Id": 327704,
"Caption": "TEST>> photo 2",
"Url": "http://bob.com/0001/327704/photo.jpg"
},
{
"Id": 327704,
"Caption": "TEST>> photo 2",
"Url": "http://bob.com/0001/327704/photo.jpg"
}
]
}
}
Spec :
[
{
"operation": "shift",
"spec": {
"Entity": {
"card": {
"cardNo": "tab.text",
"cardAddress": "address.[#2].add",
"cardAddress1": "address.[#2].add2",
"cardCity": "address.[#2].mk"
},
"Photos": {
"*": {
"Id": "Photos.[&1].no",
"Caption": "Photos.[&1].caption2"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"Photos": {
"*": {
"no": "=toString"
}
}
}
}, {
"operation": "shift",
"spec": {
"*": "&",
"Photos": {
"*": {
"no": {
"0": {
"@(2,no)": "Trash"
},
"*": {
"@(2,no)": "p.Photos.[#4].no",
"@(2,caption2)": "p.Photos.[#4].caption2"
}
}
}
}
}
}, {
"operation": "remove",
"spec": {
"Trash": ""
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"p": {
"Photos": "Photos"
}
}
}
]
CodePudding user response:
You can start by converting the independent attributes dynamically collect under common nodes, then use conditional logic such as
[
{
"operation": "shift",
"spec": {
"Entity": {
"card": {
"cardNo": "tab.text",
"*Addres*|*City": {
"*": {
"@1": "address[#1].&2"
}
},
"Id*": {// enumerate the members of the object in a dynamical manner
"*": {
"#caption2": "Photos&2[#1].http\\.&(2,1)\\.com", // &(2,1) could be used RHS
"@1": "Photos&2[#1].no"
}
}
},
"*": {
"*": {
"Id": "&2[&1].no", // rename "Id" to "no"
"*": "&2[&1].&"
}
}
}
}
},
{ // combine all objects related to Photoes under common array
"operation": "shift",
"spec": {
"*": "&",
"Photo*": {
"*": {
"@": "Photoes"
}
}
}
},
{ // label objects of photoes by their respective "no" values
"operation": "shift",
"spec": {
"*": "&",
"Photoes": {
"*": {
"http*": {
"$": "&3[&2].@(2,no).@(0)" // exchange key-value pairs
},
"*": "&2[&1].@(1,no).&"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"Photoes": {
"*": {
"0": "rem&2", // remPhotoes tagged stuff will be removed
"*": {
"@": "&3[]"
}
}
}
}
},
{
"operation": "remove",
"spec": {
"remPhotoes": ""
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"Photoes": {
"*": {
"no": "=toString"
}
}
}
}
]
CodePudding user response:
You may consider another library Josson to do the same job by shorter transformation statement.
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"{"
" \"Entity\": {"
" \"card\": {"
" \"cardNo\": \"123456789\","
" \"cardStatus\": \"10\","
" \"cardAddress\": \"UK\","
" \"cardAddress1\": \"US\","
" \"cardCity\": \"mk\","
" \"name\": \"RAM\","
" \"lastName\": \"ABU\","
" \"name1\": \"RAM1\","
" \"lastName1\": \"ABU1\","
" \"Id1\": \"0\","
" \"Id2\": 0,"
" \"Id3\": 0"
" },"
" \"Photos\": ["
" {"
" \"Id\": 0,"
" \"Caption\": \"TEST>> photo 1\","
" \"Url\": \"http://bob.com/0001/327703/photo.jpg\""
" },"
" {"
" \"Id\": 327704,"
" \"Caption\": \"TEST>> photo 2\","
" \"Url\": \"http://bob.com/0001/327704/photo.jpg\""
" },"
" {"
" \"Id\": 327704,"
" \"Caption\": \"TEST>> photo 2\","
" \"Url\": \"http://bob.com/0001/327704/photo.jpg\""
" }"
" ]"
" }"
"}");
Transformation
JsonNode node = josson.getNode(
"Entity.map("
" tab: map(text: card.cardNo),"
" address: wrap(card.map("
" add: cardAddress,"
" add2: cardAddress1,"
" cardCity:: cardCity)),"
" Photos[Id!=0]*.map("
" caption2: Caption,"
" no: toText(Id))"
")");
System.out.print(node.toPrettyString());
Output
{
"tab" : {
"text" : "123456789"
},
"address" : [ {
"add" : "UK",
"add2" : "US",
"mk" : "mk"
} ],
"Photos" : [ {
"caption2" : "TEST>> photo 2",
"no" : "327704"
}, {
"caption2" : "TEST>> photo 2",
"no" : "327704"
} ]
}