skip to Main Content

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


  1. 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 the Angular and NodeJs properties. You should directly reference the values instead.

    // Corrected payload format
    let payload = {
      abc: "abc",
      something: {
        somevalue: "somevalue",
        nestedAray: [
          { anotherValue: "anotherValue" },
          { optionalValue: "optionalValue", optionalValue1: "optionalValue1" },
        ],
      },
    };
    
    let output = {};
    let skills = [];
    
    for (let i = 0; i < payload.something.nestedAray.length; i++) {
      let skill = {};
    
      skill.JavaScript = payload.something.nestedAray[i].anotherValue;
    
      // Check if optionalValue1 exists before adding it
      if (payload.something.nestedAray[i].optionalValue1) {
        skill.NodeJs = payload.something.nestedAray[i].optionalValue1;
      }
    
      skills.push(skill);
    }
    
    output = { Java: payload.something.somevalue, skills: skills };
    
    console.log(output);
    
    Login or Signup to reply.
  2. You can just use :

    if (data.something.nestedAray[i].optionalValue1) {
          skill.NodeJs = data.something.nestedAray[i].optionalValue1;
    }
    

    to check if the option value 1 is existing in your array already.
    Refer the working code sample below :

    let correctedData = {
      abc: "abc",
      something: {
        somevalue: "somevalue",
        nestedAray: [
          { anotherValue: "anotherValue" },
          { optionalValue: "optionalValue", optionalValue1: "optionalValue1" },
        ],
      },
    };
    
    function checkForOptionValue1(data){
      let skills = [];
    
      for (let i = 0; i < data.something.nestedAray.length; i++) {
        let skill = {};
        skill.JavaScript = data.something.nestedAray[i].anotherValue;
        if (data.something.nestedAray[i].optionalValue1) {
          skill.NodeJs = data.something.nestedAray[i].optionalValue1;
        }
    
        skills.push(skill);
      }
      return { Java: data.something.somevalue, skills: skills };
    }
    
    
    console.log(checkForOptionValue1(correctedData));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search