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!

icedwater
  • 4,701
  • 3
  • 35
  • 50
user986690
  • 175
  • 1
  • 3
  • 9
  • 2
    If it's JSON and not a native Javascript object, you have to quote the key names I believe. – Jared Farrish Oct 09 '11 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. – Spudley Oct 09 '11 at 19:53
  • 1
    See: http://stackoverflow.com/questions/949449/json-spec-does-the-key-have-to-be-surrounded-with-quotes – Jared Farrish Oct 09 '11 at 19:53

2 Answers2

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?

Community
  • 1
  • 1
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
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

Martin Drapeau
  • 1,484
  • 15
  • 16