I have a nested Json Object
payload={
abc:"abc",
something:[somevalue:"somevalue",nestedAray:[{anotherValue:"anotherValue"},{optionalValue:"optionalValue",optionalValue1:"optionalValue1"}]]
}
here optionalValue1 key is optional in the payload. Need map the same JSON object to the new new Json with different Key names
ecpected output:
{
name:"abc",
skills:[Java:"somevalue","JSLibrary":[{JavaScript:"anotherValue"},{Angular:"optionalValue",NodeJs:"optionalValue1"}]]
}
I mapped the payload as
let output={};
for(i=o;i<payload.something.length;i++){
for(j=o;j<payload.something.nestedAray.length;j++){
skill[j]={
Angular:"payload.something[i].nestedAray[j].anotherValue"
NodeJs:"payload.something[i].nestedAray[j].anotherValue1" // this is optional should add only if exist to same array
}
}
}
output={Java:payload.something,skills:skill}
How to check for the optional parameter in the Nested JSON object
2
Answers
Your existing mapping seems to be incorrect as you are storing string literal
"payload.something[i].nestedAray[j].anotherValue"
and"payload.something[i].nestedAray[j].anotherValue1"
in theAngular
andNodeJs
properties. You should directly reference the values instead.You can just use :
to check if the option value 1 is existing in your array already.
Refer the working code sample below :