Home > Back-end >  convert string property from api to JSON object
convert string property from api to JSON object

Time:06-30

i'm making a call to an api which returns data to the front end in this format

{ name: 'Fred', 
data: [{'name': '"10\\" x 45\\" Nice Shirts (2-pack)"', 'price': '$30.25'}]
}

the data property is return as a string and i'm trying to use JSON.parse(response.data) to get it as as array so i can use *ngFor to do iteration but i'm always getting an error

Unexpected token ' in JSON at position 2
SyntaxError: Unexpected token ' in JSON at position 2 (at zone.js:1262:1)
    at JSON.parse

CodePudding user response:

Your backend is not returning a valid json. It should be:

{"name": "Fred", "data": [{"name": "\\"10\\\\\\" x 45\\\\\\" Nice Shirts (2-pack)\\"", "price": "$30.25"}]}
  • Related