Json parse array

1

I have the following answer

{
    "users":[
        {
            "id":1,
            "Name":"Rafael",
            "label":"teste"
        },
        {
            "id":2,
            "Name":"Carlos",
            "label":"teste"
        }
    ],
    "relations":[
        {
            "id":1
        },
        {
            "id":2
        }
    ]
}

And I want to set up two object arrays

var usersArray;
var relationsArray;

I tried it in several ways and .map(JSON.parse) does not work on my page.

    
asked by anonymous 11.04.2017 / 16:02

2 answers

4

You can store this response in a variable and separate the two arrays:

var arrJson = {
        "users":[
            {
                "id":1,
                "Name":"Rafael",
                "label":"teste"
            },
            {
                "id":2,
                "Name":"Carlos",
                "label":"teste"
            }
        ],
        "relations":[
            {
                "id":1
            },
            {
                "id":2
            }
        ]
    }

var users = arrJson.users;
var relations = arrJson.relations;

console.log(users)
console.log(relations)

In this way I have been able to separate the two Arrays without having to call any of the conversion methods

    
11.04.2017 / 16:07
2

Just assign variables to their object values:

const resposta = {
    "users":[
        {
            "id":1,
            "Name":"Rafael",
            "label":"teste"
        },
        {
            "id":2,
            "Name":"Carlos",
            "label":"teste"
        }
    ],
    "relations":[
        {
            "id":1
        },
        {
            "id":2
        }
    ]
};

var usersArray = resposta["users"];
var relationsArray = resposta["relations"];

console.log(usersArray);
console.log(relationsArray);

The output of console.log(usersArray) will be:

[
  {
    "id": 1,
    "Name": "Rafael",
    "label": "teste"
  },
  {
    "id": 2,
    "Name": "Carlos",
    "label": "teste"
  }
]

And of console.log(relationsArray) will be:

[
  {
    "id": 1
  },
  {
    "id": 2
  }
]
    
11.04.2017 / 16:08