4

I'm trying to parse JSON with jQuery I get from a remote server through AJAX. The JSON data is like: {identifier:"ID", label:"LABEL"} but unable to. Apparently, the field identifier and label has no double quotes. It works when tested on my local test site with double quotes.

Can it be made to work without quotes with jQuery? I have searched around and have found no solutions.

Any input is appreciated. Thanks.

8
  • Can you not get the JSON correctly formatted? Commented Dec 23, 2011 at 16:00
  • I'm guessing you have no control over the remote server? Passing bad JSON is something they should be fixing, even if you don't have control on that server (email them?). Can you post an example of the bad JSON? Maybe there's a regex fix or something quick and dirty if you absolutely can't change the API. Commented Dec 23, 2011 at 16:01
  • I'm afraid you'll have to use eval Commented Dec 23, 2011 at 16:02
  • Do you have to do it on the client side? Jackson can read such malformed JSON for you, but it's Java and server side. Commented Dec 23, 2011 at 16:02
  • 5
    You might have figured it out from the previous comments, but developers like being obtuse, so here it is clearly: this is not valid JSON. Creating a hack to read invalid JSON is not a good approach. You should be getting valid JSON to begin with. Commented Dec 23, 2011 at 16:06

3 Answers 3

9

Yeah, it's not valid JSON, blahblahblah... like everyone cares if it's valid or not.

At least I don't care, I just want to parse it, so I wrote jsonlite.

with Jsonlite, you can do this:

var s = '{name: jsonlite, birthday: {year: 2013, month: 7, day: 7}, isGreat: true}';
var obj = jsonlite.parse(s);

Which produces exactly the same result as the code below:

var s = '{"name": "jsonlite", "birthday": {"year": 2013, "month": 7, "day": 7}, "isGreat": true}';
var obj = $.parseJSON(s);
Sign up to request clarification or add additional context in comments.

1 Comment

nice one :) +10 for 'blahblahblah' :D
4

You can't. JSON specification says this:

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

Meaning string as the label you mention.

Source: http://www.json.org/

Comments

0

Using a regular expression the invalid JSON can be made valid JSON. In example below the sFixed will be valid JSON, which can be parsed: .

let s = '{identifier:"ID", label:"LABEL"}',
    sFixed = s.replace(/(['"])?([a-zA-Z0-9_][a-zA-Z0-9_\s]*[a-zA-Z0-9_])(['"])?:/g, '"$2": '), //'{"identifier":"ID", "label":"LABEL"}'
    obj = JSON.parse(sFixed);

1 Comment

A code block alone does not provide a good answer. Please add explanations (why it solve the issue, where was the mistake, etc...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.