2

I am building a web-application (using Javascript/jQuery) that generates JSON configuration files.

At the end the applications should convert the generated JSON to a string so that it can be copied by the user?

At the moment I am doing a simple JSON.stringify(), but this adds quotation to both keys and values. In order to get the configuration files to work it is necessary that the keys are exported without quotes (unless stated).

For example, "id": "ezdzdz" should be printed as id: "ezdzdz" unless its actually written as "id" in the JSON.

Thanks, all help is much appreciated!

3
  • 2
    If it's JSON and not a native Javascript object, you have to quote the key names I believe. Commented Oct 9, 2011 at 19:52
  • 3
    In JSON syntax, the quotes are mandatory. They may be optional in Javascript itself, but for it to be valid JSON, it needs to have the quotes. Commented Oct 9, 2011 at 19:53
  • 1
    See: stackoverflow.com/questions/949449/… Commented Oct 9, 2011 at 19:53

2 Answers 2

5

If it's JSON and not a native Javascript object, you have to quote the key names.

See: JSON Spec - does the key have to be surrounded with quotes?

Sign up to request clarification or add additional context in comments.

Comments

1

Embedding a JSON object in Javascript without quotes on keys is valid. It isn't valid JSON, but it is valid Javascript. Useful for stub data in unit tests for example.

I wrote a tool to do that called JSON Beautifier. Here it is: http://www.csvjson.com/json_beautifier

3 Comments

This is nice, but it still quotes the keys even if the box is checked on an array of objects.
Indeed. A bug to fix. I'll reply here when it's fixed.

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.