cancel
Showing results for 
Search instead for 
Did you mean: 

Transform values of a key into an object with list of the value

JensDeveloper
Contributor II

Hi,

So I am trying to change my json data in a way that we have a list of companies that show every record with the name of the company in an object with the required fields
My input:
{
"companies":{
"CURRENT_COMPANY_DISPLAY_NAME":"test1",
"Code":"123",
"Company_Legal_Name":"test1 nv",
"Company_Name":"test1 x",
"Country":"Germany"
}
},
{
"companies":{
"CURRENT_COMPANY_DISPLAY_NAME":"test2",
"Code":"456",
"Company_Legal_Name":"test2 NV",
"Company_Name":"test2 x",
"Country":"Belgium"
}
}

My requested output:

{
  "companies": {
    "test1": {
      "Code": "123",
      "Company_Legal_Name": "test1",
      "Company_Name": "test1 nv",
      "Country": "germany",
    },
    "test2": {
      "Code": "456",
      "Company_Legal_Name": "test2",
      "Company_Name": "test2 nv",
      "Country": "belgium",
    }
  }
}

 I have tried the mapping of the values and the keys but haven't got really further yet.

Regards

Jens

1 ACCEPTED SOLUTION

alchemiz
Contributor III

Hi @JensDeveloper ,

Good day, hope this helps 😊

$.map(c=> sl.ensureArray(c['companies']['CURRENT_COMPANY_DISPLAY_NAME']).toObject((x,y)=> x, (x,y)=> c.get('companies').filter((v,k)=> k != 'CURRENT_COMPANY_DISPLAY_NAME')))

 

alchemiz_0-1697765912734.png

~EmEm

View solution in original post

2 REPLIES 2

alchemiz
Contributor III

Hi @JensDeveloper ,

Good day, hope this helps 😊

$.map(c=> sl.ensureArray(c['companies']['CURRENT_COMPANY_DISPLAY_NAME']).toObject((x,y)=> x, (x,y)=> c.get('companies').filter((v,k)=> k != 'CURRENT_COMPANY_DISPLAY_NAME')))

 

alchemiz_0-1697765912734.png

~EmEm

Thank you @alchemiz  this works 🙂