Search code examples
javascripthtmljqueryjsonbootstrap-tags-input

Parse an unserialized JSON object array with commas in values


I am using tag inputs to submit a string representation of 0 or more objects. The input value in the form is a comma separated list of stringified JSON objects.

Example:

var value = "{}, {}, {}, {}"

Each of the individual objects have a field containing a name: {..., name: "Doe, John"}

Because this comma is here I cannot execute JSON.parse(value)

Unexpected token , in JSON at position ...

I want to avoid doing any string split methods because of undesired splitting of names.


Solution

  • Add brackets to the string and it will parse correctly

    var value = "{}, {}, {}, {}, {}"
    JSON.parse("[" + value + "]")