I have a list that I want to publish as JSON, so that others can easily reuse it:

product1: price1
product2: price2
product3: price3
product4: price4

What is the most reusable way to publish this data as JSON?
productX and priceX are strings.

I thought I would use the same format as this JSON list of country names/codes, but it does not even validate on JSONLint:

Parse error on line 2:
[    {        name: 'Afghanistan',
Expecting 'STRING', '}'

For some reason everyone seems happy to use though, so I might be misunderstanding something...

  {name: 'Afghanistan', code: 'AF'}, 
  {name: 'Albania', code: 'AL'}
  • 1
    This purely programming related question is a better fit for StackOverflow. If possible, please add some Open Data related aspects to your question. Jan 16, 2015 at 10:26
  • StackOverflow does not care about reusability. Also, StackOverflow is for programming question, and recently frowns upon any new question that does not contain source code. JSON is not source code, it is data.
    – Nicolas Raoul
    Jan 16, 2015 at 10:52
  • 1
    JSON5 would have caused no parse error, since it is compatible with JavaScript. Unfortunately, it is not very popular yet (I hope that this will change soon) Jan 28, 2015 at 23:03

1 Answer 1


Your sample data as well as the provided list of country codes are valid JavaScript, but they are not JSON. To make them valid JSON, put all strings in double quotes:

  {"name": "Afghanistan", "code": "AF"}, 
  {"name": "Albania", "code": "AL"}

This answer on StackOverflow gives a nice overview of the differences between JavaScript and JSON.

Regarding your initial question, your sample data would look like this:

    {"product1": "price1"},
    {"product2": "price2"},
    {"product3": "price3"},
    {"product4": "price4"}

In most cases1 it's probably better to make your prices actual numbers instead of strings that just look like numbers:

    {"product1": 123.45},
    {"product2": 234.56},
    {"product3": 345.67},
    {"product4": 456.78}

If you think about adding more properties to your products in the future, you should probably use this instead:

        "name": "product1",
        "price": 123.45
        "name": "product2",
        "price": 234.56
        "name": "product3",
        "price": 345.67
        "name": "product4",
        "price": 456.78

1 As Walter Tross pointed out in the comments, there are edge cases when rounding errors can occur. For example, if you have a product that costs 456789.99, it might happen that the computer program that reads your JSON will actually calculate the price as 456790.00. There is currently no simple solution for this problem. If you think this might be relevant for you, you might want to read up on some related discussions and the general problem of the precision of floating point numbers.

  • Prices as numbers is ok if you know how many decimal digits you need when converting the numbers back to strings, and if there are not too many digits, including decimal digits (max: ca. 7 for single precision, ca. 16 for double precision). If any of these conditions is not met, strings are a better choice. Jan 28, 2015 at 22:50
  • Now I'm curious: Could you please provide a realistic example of a product price that could or should not be encoded as a JSON number? Jan 28, 2015 at 23:04
  • If you use single precision floats (32 bits IEEE 754 floating point numbers, quite common), 1234567.89 gets converted back to string as 1234567.88 (I just checked). Jan 28, 2015 at 23:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.