0

I am facing an issue in small requirement. I want to create a JSON array in below format using JavaScript for loops.

var mainJson = {
        "people": [{
                "name": "Ravi",
                "role": "team member",
                "appointments": [{
                        "info": "room 1",
                        "type": "Type01"
                        "title": "Meet John Miller"
                    }, {
                        "info": "room 2",
                        "type": "Type02"
                        "title": "Meet Mitchell"
                    }]
            ]
        }
    };

To create above JSON, I have 2 jsons like below..

var subJson1 = [{
            "RowId": "00272727",
            "Rowlabel": "Ravi",
            "role": "team member"  
}]

var subJson2 = [{
      "info": "room 1",
      "RowId": "00272727",
      "type": "Type01",
      "title": "Meet John Miller"
      }, {
      "info": "room 2",
      "type": "Type02",
      "RowId": "00272727",
      "title": "Meet Mitchell"
      }]

Here in both SubJson1 and subJson2 "RowID" is the common parameter. Based on RowId we have to create mainJson .In the mainJson in people array objects will be more than 1. im just displaying only one item for understanding.

Im trying to achieve this with for loops. But im facing issues to resolve this. Can some one help me to create the mainJson using subJson1 and subJson2

Thank you in advance.

user2644620
  • 199
  • 1
  • 10
  • 37
  • 2
    How do you know which `subjson2` value will match with which `subjson1` value – brk May 15 '18 at 15:07
  • 1
    JSON is a string format to represent javascript objects. What you have here are javascript object and array literals, not JSON. – Yury Tarabanko May 15 '18 at 15:09
  • My Mistake, I edited my question. Here based on RowId in subJsons i need to form main JSON. Can you please check now – user2644620 May 15 '18 at 15:41
  • @user3496976 please check my answer. It works as you expecting. If there's something misunderstood, please feel free to ask me. – Jordan Enev May 15 '18 at 20:13

4 Answers4

0

Will the sub JSON values always come through in the same format? If so you could skip the hassle of for loops and just pass the values through a function.

EDIT: Please see the edited answer below.

const subJson1 = [
    {
        "RowId": "00272727",
        "Rowlabel": "Ravi",
        "role": "team member"  
    }
]

const subJson2 = [
    {
    "info": "room 1",
    "RowId": "00272727",
    "type": "Type01",
    "title": "Meet John Miller"
    }, 
    {
    "info": "room 2",
    "type": "Type02",
    "RowId": "00272727",
    "title": "Meet Mitchell"
    }
]

const consolidateJSON = (subJson1, subJson2) => {
    return {
        people: [
            {
                name: subJson1[0].Rowlabel,
                role: subJson1[0].role,
                appointments: subJson2.map( item => {
                    const {info, type, title} = item;
                    return {
                        info,
                        type,
                        title
                    }
                } )
            }
        ]
    }
}
gnusey
  • 354
  • 3
  • 16
0

Here's how you can map people with appointments by RowId, using ES6:

const subJson1 = [{
  "RowId": "00272727",
  "Rowlabel": "Ravi",
  "role": "team member"  
}]

const subJson2 = [{
  "info": "room 1",
  "RowId": "00272727",
  "type": "Type01",
  "title": "Meet John Miller"
  }, {
  "info": "room 2",
  "type": "Type02",
  "RowId": "00272727",
  "title": "Meet Mitchell"
}]

/**
 * Get people, mapped with their appointments
 * 
 * @people {Object[]}
 * @appointments {Object[]}
*/
const getPeopleWithAppointments = (people, appointments) => ({
  people: people.map(person => ({
    ...person,
    appointments: appointments.filter(({ RowId }) => RowId === person.RowId)
  }))
})

console.log(getPeopleWithAppointments(subJson1, subJson2))
Jordan Enev
  • 16,904
  • 3
  • 42
  • 67
0

Try this :

var subJson1 = [{
  "RowId": "00272727",
  "Rowlabel": "Ravi",
  "role": "team member"  
}];

var subJson2 = [{
  "info": "room 1",
  "RowId": "00272727",
  "type": "Type01",
  "title": "Meet John Miller"
}, {
  "info": "room 2",
  "type": "Type02",
  "RowId": "00272727",
  "title": "Meet Mitchell"
}];

var mainJson = {"people" : []};

mainJson.people.push({"appointments" : subJson2.map(item => {
    delete item.RowId;
    return item;
  })
});

mainJson.people[0].name = subJson1[0].Rowlabel;
mainJson.people[0].role = subJson1[0].role;

console.log(mainJson);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
-1

If you are simply trying to merge two objects, there's a lot of utilities that do this, which I would defer to. SO Questions such as this also offer several ways to do it.

You, however, don't seem to be looking for that. In your case, you seem to want to create a new object with a new structure and insert the other two objects into it. For this, you would not use loops, but simply do the following:

var subJson1 = {
    "RowId": "00272727",
    "Rowlabel": "Ravi",
    "role": "team member"  
}

var subJson2 = [
    {
        "info": "room 1",
        "type": "Type01"
        "title": "Meet John Miller"
    },
    {
        "info": "room 2",
        "type": "Type02"
        "title": "Meet Mitchell"
    }
];

var mainJson = {
    "people": [{
        "name": subJson1.Rowlabel,
        "role": subJson1.role,
        "appointments": subJson2
    }]
};

Keep in mind; you don't need to put quotes around your keys in Javascript (.js files), only in JSON (.json files).

Ian Paschal
  • 772
  • 4
  • 13
  • My Mistake, I edited my question. Here based on RowId in subJsons i need to form main JSON. Can you please check now – user2644620 May 15 '18 at 15:41