parse [object Object] Angular 6

json in angular 6
how to create json object in angular 6
angular 4 convert object to json
how to create json object in angular 4
convert json object to array angular 6
how to get data from json object in angular 6
object object in angular 7
the pipe 'json' could not be found

I want to parse a string and save projectName and poNumber into 2 variables. This is what I have by now, using JSON.parse()

employees = []
JSON.parse(data).array.forEach(element => {
    this.employees.push({
      projectName: element.projectName,
      poNumber: element.poNumber
    })
  });

console.log(employees['projectName'])
console.log(employees['poNumber'])

where data has this format:

{"id":1,"name": "john doe", "project":"[object Object]"}

and project looks like:

"project": [
     {
        "projectName": "proj1",
        "poNumber": "1"
     }
]

But I get this error

ERROR SyntaxError: Unexpected token o in JSON at position 1

Where am I mistaking? Thank you for your time!

EDIT: I understood why I get this error, because my data is already an object and there is no need to use JSON.parse(), but my code is still not working because I get the error:

core.js:1671 ERROR TypeError: Cannot read property 'forEach' of undefined

Your Json String is not valid and is not stringified by JSON.stringify() instead it was stringified by toString method, otherwise nested objects were stringified properly.

JSON.parse() and JSON.stringify(), stringify() takes a JavaScript object and transforms it into a JSON string. Here's an example: const myObj = { name: 'Skip',  Papa Parse wrapper for Angular. Contribute to alberthaff/ngx-papaparse development by creating an account on GitHub.

The problem was that I didn't parse the right thing. Here is the correct snippet:

let p = JSON.parse(data.project);

  this.employees.push({
  projectName: p.projectName,
  poNumber: p.poNumber
})

Thank you everyone for help! Every answer of you was right.

JsonPipe, The following component uses a JSON pipe to convert an object to JSON format, and displays the string in both formats for comparison. @Component({ selector:  The JSON object, available in all modern browsers, has two very useful methods to deal with JSON-formatted content: parse and stringify. JSON.parse () takes a JSON string and transforms it into a JavaScript object. JSON.stringify () takes a JavaScript object and transforms it into a JSON string. Here’s an example:

Try like this :

JSON.parse(data).array.forEach(element => {
  this.employees.push({
    projectName: element.project.projectName,
    poNumber: element.project.poNumber
  })
})

How To Parse And Stringify JSON Data Using Angular, stringify(), which is used to convert the object data into the JSON format. Example​. import { Component , OnInit } from '@angular/core'  The problem was that I didn't parse the right thing. Here is the correct snippet: let p = JSON.parse(data.project); this.employees.push({ projectName: p.projectName, poNumber: p.poNumber }) Thank you everyone for help! Every answer of you was right.

I was getting same error in JSON format, but after validating it on online JSON validator it seems perfect. On the basis of that I have created following example which shows two diffrent ways to get data in your "employees" array.

I hope this will be helpful.

let data = {
	"id":1,"name":"john doe",
  "project":[
        {
		"projectName": "proj1",
		"poNumber": "1"
	},
        {
		"projectName": "proj2",
		"poNumber": "2"
	}
    ]
};

let employees = [];


Object.keys(data).map(key => {
    if(key === 'project') {
        // Method 1
        // ===========================
        data[key].map(obj => {
            employees.push(obj);
        });
        
        // Method 2 : Shorter method
        // ===========================
        employees.push(...data[key]);
    }
});

console.log('Employees :', employees);

How to read [object object] using angular-2 - ionic-v3, How to read [object object] using angular-2 · ionic · ionic-v3 · el0zahaby June 25, 2019, 6:10am #1 jsonObject = JSON.parse(jsonData); } }. Ultimately, Angular is performing a JSON.parse on the body of the response. Part of the issue is that TypeScript is blissfully unaware that Angular will not instantiate the object and treats the return as a Promise<Details> .

JSON.parse(data).array.forEach(element => {
     this.employees.push({
        projectName: element.project.projectName,
        poNumber: element.project.poNumber
     })
});

JSON.parse(), The JSON.parse() method parses a JSON string, constructing the JavaScript 6. ​. 7. console.log(obj.result);. 8. // expected output: true. 9. ​. Run › See the JSON object for a description of JSON syntax. reviver Optional: If  If you don’t know it yet. I’m talking about Angular 2+. FYI: Here is a post about why it is just Angular. So you want to display the markup for a list, the values for this list come from the

How to Work with JSON.stringify() and JSON.parse() in JavaScript?, As for a value, it can be an object, array, Boolean expression, number or string. Key/Value pair: It has a specific syntax. It is represented like this ->  Angular is a platform for building mobile and desktop web applications. Join the community of millions of developers who build compelling user interfaces with Angular.

Angular Http Tips For Success, parse will give you an object and because of JavaScript coercion, it works. TypeScript will treat it as this object and everything is perfect. export  If its a JSON you are getting back from your service.ts GET method, you can JSON.parse() that complete JSON string. After that, you can simply access the array from that object you got returned from JSON.parse().

Typescript, Typescript - How to Convert Object to Array or Array to Object| Angular Example. Typescript Object Array. Typescript is superset of javascript with compile type  Well, it feels like only yesterday we were playing with AngularJS(1) and along came the new Angular(2,4) framework. One of the pretty nifty things we could do in AngularJS is using parsers and…

Comments
  • whats is [object Object] in data?
  • This is how my entry looks into my database, I posted it using postman, but project has the format I wrote in my question.
  • Can you add console in element and post consoled data into your question.
  • try data.project.forEach.
  • use JSON.parse(data.project). //logic and check project list is valid or not. ie. it is properly formatted or not using JSON.stringify()
  • Thank you, you made me realize what I did wrong. I have to save the data first with JSON.stringify(x)
  • Your Most welcome :) Please mark answer as accepted.
  • A good answer has a better explanation How do I write a good answer?