0

I have an input on a form which stores values as an object.

jQuery('#inputId').val()

returns something like

'[{"Id":"123","Name":"A","PathOfTerm":"A","Children":[],"Level":0,"RawTerm":null},{"Id":"234","Name":"B","PathOfTerm":"B","Children":[],"Level":0,"RawTerm":null}]'

as one single string. Is there any way to either prevent this from automatically converting to a string (maybe not using .val?) or to convert this from a string to something I could work with?

George
  • 6,630
  • 2
  • 29
  • 36
cooper
  • 417
  • 1
  • 10
  • 20
  • So you want to have it as a `object` ? – David R May 16 '17 at 13:03
  • 4
    you could `JSON.parse` this – thedude May 16 '17 at 13:04
  • Looks like it's returning [JSON](http://www.json.org/). Lucky for you most browsers have the function [`JSON.parse`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) to convert it to an object – George May 16 '17 at 13:04
  • Possible duplicate of [Parse JSON in JavaScript?](http://stackoverflow.com/questions/4935632/parse-json-in-javascript) – J. Titus May 16 '17 at 13:06
  • Possible duplicate of [Convert JSON string to array of JSON objects in Javascript](http://stackoverflow.com/questions/4375537/convert-json-string-to-array-of-json-objects-in-javascript) – jeffdill2 May 16 '17 at 13:06
  • Possible duplicate of [Safely turning a JSON string into an object](http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) – freedomn-m May 16 '17 at 13:18
  • It's not automatically converting to a string, form element values are always strings – Lennholm May 16 '17 at 13:37

1 Answers1

5

Here you go

var array = JSON.parse(jQuery('#inputId').val());
schaffioverflow
  • 510
  • 3
  • 14