Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nested JSON, create new array with values from multiple levels?

I have this nested json object but I would like to construct a new one by taking elements of the original. I would like to make the second array below using the first, where the id key value becomes the key for a list of the emailAddress values in each permissionsOriginal:

        [
        {
            "id": "1yKKftO0iOyvsacrW1mEr-tw43ttw3-8IorkDwiaYLgqI",
            "name": "Doc Control",
            "permissions": [
                {
                    "emailAddress": "pkenny@cheese.co",
                    "role": "writer",
                    "displayName": "Bob Kenny"
                },
                {
                    "emailAddress": "nDrape@cheese.co",
                    "role": "writer",
                    "displayName": "Nute Drape"
                }
            ]
        },
        {
            "id": "149Lmt-g3w4w3efgh6thyherawer443awt3wrwa3rewrwa",
            "name": "Untitled document",
            "permissions": [
                {
                    "emailAddress": "pkenny@cheese.co",
                    "role": "owner",
                    "displayName": "Bob Kenny"
                }
            ]
        },
        {
            "id": "egrs54h6w4hgwe5wegrgwegrhterwwrttre-Uffk8QRg4",
            "name": "Documentation Control test",
            "permissions": [
                {
                    "emailAddress": "wDragon@cheese.co",
                    "role": "writer",
                    "displayName": "Grape Dragon"
                },
                {
                    "emailAddress": "pkenny@cheese.co",
                    "role": "owner",
                    "displayName": "Bob Kenny"
                }
            ]
        }
        
        ]

Second Snippet:

                        [
    {
        "1yKKftO0iOyvsacrW1mEr-tw43ttw3-8IorkDwiaYLgqI": [
            "pkenny@cheese.co",
            "nDrape@cheese.co"
        ]
    },
    {
        "149Lmt-g3w4w3efgh6thyherawer443awt3wrwa3rewrwa": [
            "pkenny@cheese.co"
        ]
    },
    {
        "egrs54h6w4hgwe5wegrgwegrhterwwrttre-Uffk8QRg4": [
            "wDragon@cheese.co",
            "pkenny@cheese.co"
        ]
    }
]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Loop through each element, then loop through permissions and extract the email.

   let resp = [{
       "id": "1yKKftO0iOyvsacrW1mEr-tw43ttw3-8IorkDwiaYLgqI",
       "name": "Doc Control",
       "permissions": [{
           "emailAddress": "pkenny@cheese.co",
           "role": "writer",
           "displayName": "Bob Kenny"
         },
         {
           "emailAddress": "nDrape@cheese.co",
           "role": "writer",
           "displayName": "Nute Drape"
         }
       ]
     },
     {
       "id": "149Lmt-g3w4w3efgh6thyherawer443awt3wrwa3rewrwa",
       "name": "Untitled document",
       "permissions": [{
         "emailAddress": "pkenny@cheese.co",
         "role": "owner",
         "displayName": "Bob Kenny"
       }]
     },
     {
       "id": "egrs54h6w4hgwe5wegrgwegrhterwwrttre-Uffk8QRg4",
       "name": "Documentation Control test",
       "permissions": [{
           "emailAddress": "wDragon@cheese.co",
           "role": "writer",
           "displayName": "Grape Dragon"
         },
         {
           "emailAddress": "pkenny@cheese.co",
           "role": "owner",
           "displayName": "Bob Kenny"
         }
       ]
     }

   ]
   let res = [];
   resp.forEach((data) => {
     let emailArray = [];
     let resObj = {};
     data.permissions.forEach((permData) => {
       emailArray.push(permData['emailAddress']);
     })
     resObj[data.id] = emailArray;
     res.push(resObj);
   })
   console.log(res);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading