1

In my current Angular project I send a JSON file to a server and the server gives me back a modified JSON file. The new JSON file should be displayed in a tree.

My current problem is that I cannot read the data from the new JSON. Whenever I want to display a JSON Object, I always get the error:

[object Object]

Post request:

public sendJSON() {
    this.http.post('url', this.combineJSON,{headers:{'Content-Type': 'application/json'}}).subscribe((data: Device) => {

    console.log("Test Output: " + data);
  })
}

I defined a model to work with the response:

export class Device {
    orderNumber: number;

    deviceTypeId: number;

    swRevision: string;

    hwRevision: string;
}

Where is the problem here that the data is not displayed correctly?

3
  • console.log("Test Output: ", data) Commented Jan 30, 2020 at 9:31
  • There's no such thing as a JSON Object. It's either JSON, i.e. text, or an Object. To see proper console output, use console.log("Test Output: ", data); Commented Jan 30, 2020 at 9:31
  • 1
    See also here: stackoverflow.com/questions/4750225/… Commented Jan 30, 2020 at 9:33

2 Answers 2

3

The data not being correctly displayed is because of the + in your console.log.

+ concatenates strings and, in the case of something other than a string, calls the toString() method of the object.

Simply change:

console.log("Test Output: " + data);

to:

console.log("Test Output: ", data);

and it should display your object.

Sign up to request clarification or add additional context in comments.

Comments

3

You are trying to print a whole object. You can utilize JSON.stringify() function to display your object as a string.

console.log("Test Output: " + JSON.stringify(data));

The model you defined helps you to identify all properties of data object in your source code. As an example you could print the swRevision property with:

console.log("Test Output: " + data.swRevision);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.