Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Json Manipulation if json key having more than one fields

I have a json like this:

[
    {
        "objectid":"61b1",
        "name":["abc","xyz"],
        "date":["2021-08-20","2021-09-20"]
    }
]

I want to manipulate the json to create a simple one as below:

[
    {
        "objectid":"61b1",
        "name":"abc",
        "date":"2021-08-20"
    },
    {
        "objectid":"61b1",
        "name":"xyz",
        "date":"2021-09-20"
    }
]

Please help if anyone has done this earlier. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use reduce to split date and name for each element and push the splite value to the accumulated result as follow:

var input = [
    {
        "objectid":"61b1",
        "name":["abc","xyz"],
        "date":["2021-08-20","2021-09-20"]
    }
];


var output = input.reduce(function(acc, curr){
    //foreach element we try to slipt it 
    // if we have more than one element in date or name 

    if(curr.name.length > 0 && curr.date.length > 0){
        //we make sur that we the same length of date et name        
        var length = Math.min(curr.name.length, curr.date.length);
        for(var i = 0; i < length; i ++){
           var name = curr.name[i];
           var date = curr.date[i];
          
           acc.push({
               "objectid": curr.objectid,
               "name": name,
               "date": date 
           });
        }

    }else{
        acc.push(curr);
    }
    return acc;
},[])

output;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!