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

Unfolding JsonArray Response object

So I am trying to unravel my response from a fetch:

  fetch('restservices/users/', fetchOptions)
    .then(function (response) {
      if (response.ok) {
        console.log('1');
        console.log(response)
        const responseSjon = response.json()
        console.log('2');
        console.log(responseSjon);
        console.log('3');
        // console.log(JSON.stringify(responseJson, null, 2));
        console.log(JSON.stringify(responseJson));
        console.log('4');

In my Javascript I unpack the response as a json with response.json.
Then I console.log to my browser, and the (raw) response in the console is as such:
enter image description here

[
   {
      "email":{
         "chars":"CasaSuCasa",
         "string":"CasaSuCasa",
         "valueType":"STRING"
      },
      "naam":{
         "chars":"Coyote",
         "string":"Coyote",
         "valueType":"STRING"
      },
      "hoeveelheid zoekopdrachten":{
         "integral":true,
         "valueType":"NUMBER"
      }
   },
   {
      "email":{
         "chars":"syd@barrett.com",
         "string":"syd@barrett.com",
         "valueType":"STRING"
      },
      "naam":{
         "chars":"CrazyDiamond",
         "string":"CrazyDiamond",
         "valueType":"STRING"
      },
      "hoeveelheid zoekopdrachten":{
         "integral":true,
         "valueType":"NUMBER"
      }
   }
]

Am I correct in saying that it looks to be an array containing JsonArray objects, containing json objects?

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

My question is how do I reach this objects individually in a console.log(xxx); ?

EDIT:
This is what the rest service on the other side, I have access to both:

            Map<String, User> commune = Community.getUserMap();
            JsonArrayBuilder jab = Json.createArrayBuilder();

            JsonObjectBuilder job = Json.createObjectBuilder();
            commune.forEach((key, user) -> {

                job.add("email", user.getEmail());
                job.add("naam", user.getNaam());
                job.add("role", user.getRole());
                job.add("hoeveelheid zoekopdrachten", user.getAlleZoekertjes().size());
                jab.add(job);
            });
            return ok(jab.build()).build();
        }

>Solution :

log object in a formatted way using JSON.stringify

console.log(JSON.stringify(responseJson, null, 2));

or in your specific case, use this:

fetch('restservices/users/', fetchOptions)
.then(response=>response.json())
.then(response=>console.log(JSON.stringify(response, null, 2)))
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