convert a JSON to a string array in typescript

22,459

Solution 1

If the response is what you included then it's simple:

JSON.parse(data.text()).forEach(item => {
    console.log(item.name);
});

As your body is an array of objects of this interface:

interface ObjectInResponseArray {
    name: string;
}

Solution 2

First off, you're mixing up JSON and objects. JSON is a text format for transferring data. Once you parse it (JSON.parse) you've got an object or an array, depending on the contents of the JSON.

var json = "{\"key\": \"value\" }";
var object = JSON.parse(json);
json = "[1, 2, \"three\"]";
var array = JSON.parse(json);

Next, it looks like you already have an object. If you do JSON.stringify, you're converting an object into a string. So you can access your data object directly. No need to convert one way or the other.

console.log(data._body[0]);

Are you familiar with for loops? They're the most common way of iterating through an array in JavaScript (and most languages, actually). For example:

var objs = [
  {
    name: 'Bugs'
  },
  {
    name: 'Bunny'
  }
];
for (var i = 0; i < objs.length; i++) {
  var obj = objs[i];
  console.log(obj.name);
}

Given all of that, you should be able to iterate over your data._body array and access the name property on each element.

Since your question is a bit unclear, it's possible you'll need to iterate over the parsed contents of data.text().

var objs = JSON.parse(data.text());
// Use a simple loop like above
Share:
22,459
malachi54
Author by

malachi54

Updated on July 09, 2022

Comments

  • malachi54
    malachi54 almost 2 years

    I feel like I have quite an easy question but I just don't get to the answer... I parse the .text() of a response from my server with JSON.parse into JSON. And i actually can access the values of that JSON with: this.temp[0].name. Now i just want to iterate over that JSON and push the values into a string array. But how can i achieve this?

    When i attempt to use for...of the compiler wants a string or an array but not a JSON. When i attempt to use for...in it actually does not go into the loop.

    Here is some code where i can access it fine:

           this._pS.getAllProjects()
            .subscribe(data => this.temp = JSON.parse(data.text()),
                err => console.log(err),
                () => console.log("hello " + this.temp[0].name + this.temp[1].name));
    

    When i just print it out after .stringify():

            this._pS.getAllProjects()
            .subscribe(data => this.temp = JSON.stringify(data),
                err => console.log(err),
                () => console.log("hello " + this.temp));
    

    i get this in the browser console:

    {"_body":"[{\"name\":\"Projekt A\"},{\"name\":\"Projekt XYZ\"}]",
    "status":200,
    "ok":true,
    "statusText":"Ok",
    "headers":{"Content-Type":["application/json; charset=utf-8"]},
    "type":2,
    "url":"http://127.0.0.1:3000/api/getAllProjectNames"}
    

    tl;dr: how can i get the values out of text part of the body into a string array?

    Thanks in advance