0

This code :

JSON.parse("['Testing avionics, electrical and mission wiring, verifying the correct installation of cabling, solving harness']")

Return this error :

VM2642:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1
    at JSON.parse (<anonymous>)
    at <anonymous>:1:6

But this code works perfectly :

var arr = ['Testing avionics, electrical and mission wiring, verifying the correct installation of cabling, solving harness']

So is there a better way to parse than JSON? The only solution I see is to make my own parser.

I'm using google chrome v 66.0.3359.181

François MENTEC
  • 1,150
  • 4
  • 12
  • 25
  • 4
    JSON requires double quotes, not single. – Joe May 24 '18 at 12:18
  • `JSON.parse('["Testing avionics, electrical and mission wiring, verifying the correct installation of cabling, solving harness"]')` will work – Pankit Kapadia May 24 '18 at 12:19
  • [JSON](http://json.org) uses double quotes (`"`) to enclose the strings. Your input is not valid JSON. – axiac May 24 '18 at 12:38
  • 1
    *"But this code works perfectly :"* -- that code is JavaScript, not JSON. JSON is a subset of JavaScript and the strings enclosed in single quotes are JavaScript but not part of JSON. – axiac May 24 '18 at 12:40

1 Answers1

1

There is no need to write your own parser. In javascript double quotes json is standard json format. So you can use double quotes inside your json and wrap it in a single quote for it to be parsed as valid JSON.

var arr = JSON.parse('["Testing avionics, electrical and mission wiring, verifying the correct installation of cabling, solving harness"]');
console.log(arr);
Rohit Agrawal
  • 1,496
  • 9
  • 20