Click here to Skip to main content
15,885,920 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi
I have this array:

[
   {
      "role":{
         "code":"LA",
         "name":"Local administrator",
         "id":1
      }
   },
   {
      "role":{
         "code":"SA",
         "name":"System administrator",
         "id":2
      }
   }
]


and also the scopes array
"scopes":[
         {
            "type":{
               "id":1,
               "code":"COR",
               "name":"Description 1"
            },
            "ids":[
               1,
               2,
               3
            ],
            "cascade":false
         }
      ],"scopes":[
         {
            "type":{
               "id":2,
               "code":"TES",
               "name":"Test"
            },
            "ids":[
               55
            ],
            "cascade":true
         },
         {
            "type":{
               "id":1,
               "code":"COR",
               "name":"Description 1"
            },
            "ids":[
               77,
               88
            ],
            "cascade":true
         }
      ]


and an array of the the RoleId:
"roleId":1, "roleId":2


How can I combine all those to end up to the following JSON structure?

[
   {
      "role":{
         "code":"LA",
         "name":"Local administrator",
         "id":1
      },
      "scopes":[
         {
            "type":{
               "id":1,
               "code":"COR",
               "name":"Description 1"
            },
            "ids":[
               1,
               2,
               3
            ],
            "cascade":false
         }
      ],
      "roleId":1
   },
   {
      "role":{
         "code":"SA",
         "name":"System administrator",
         "id":2
      },
      "scopes":[
         {
            "type":{
               "id":2,
               "code":"TES",
               "name":"Test"
            },
            "ids":[
               55
            ],
            "cascade":true
         },
         {
            "type":{
               "id":1,
               "code":"COR",
               "name":"Description 1"
            },
            "ids":[
               77,
               88
            ],
            "cascade":true
         }
      ],
      "roleId":2
   }
]


What I have tried:

I tried with array.push but it's not working properly
Posted
Comments
Member 15627495 6-Jun-22 3:45am    
Hello !

array.push(element) add 'element' at end of the array.

you search to combine your datas at the good place in the array.

it require to fetch the array, and to add datas at the good place.

JSON datas are strings first, but behave like n-dimensions array, you got root / and so sub levels themselves containing others sub levels of datas.

n-dimensionals arrays are {//root. {sub.. {subsub... }},{sub.. }, {sub.. {subsub... }} }
JSON is multi-datas type , there are all types of datas in ( strings , integers , objects ), sometimes functions too for scripting execution.

It's reading a 'hierarchy' because the root contains all elements.
each elements contains datas to read.

you can use 'index' with i++ to fetch the root , then reach a sub depth with a second loop, or a JSON parser/reader.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900