-5

I think i didnt explain this well, this is edited.

{"code":200,"meta":"{item:{Title:'{\"text\":\"This is title.\"}', Description:['Description']}}","name":"Awesome Sword's Information","id":10442}

I want to get this object's meta. but I only could get {item:{Title:'{"text":"This is title."}', Description:['Description']}}. How can I convert this string to javascript object? Thanks.

  • 1
    You want to covert JSON to a JS object? A rest API would normally require JSON not a JS object. But to convert JSON to JS object you can do `JSON.parse()`. To go from JS object to JSON you do `JSON.stringify()` – volume one Jul 26 '20 at 09:21
  • @volume one this is required by own item system I dunno what to do but the json will return that on `object.meta` – ilsubyeega Jul 26 '20 at 10:27
  • so you want `const myObject = JSON.parse(yourJSON) ` then do `myObject.meta` but your JSON string does not look like proper JSON. Wherever you got the JSON from needs to be reviewed. Make sure you are getting correctly formatted JSON first or else the `JSON.parse()` won't work – volume one Jul 26 '20 at 18:01

2 Answers2

0

You can use JSON.parse to get object from string

const a = {result:{Title:'{"text":"This is title."}', Description:['Description']}}

const result = JSON.parse(a.result.Title)

console.log(result)
AlexAV-dev
  • 1,165
  • 4
  • 14
  • I think the problem is that the `meta` key is invalid JSON, so the question is how one turns it from that format to something that JavaScript can parse. – halfer Jul 26 '20 at 11:56
0

JSON parsing is natively supported with JSON object. Try this:

var obj = JSON.parse(text); 
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Andrea Del Bene
  • 2,521
  • 1
  • 15
  • 20
  • The problem though is that although the JSON is valid, it contains a `meta` key that contains an invalid JSON string. I believe the OP wishes to parse that, and `JSON.parse` won't cope with it. – halfer Jul 26 '20 at 11:54