39

JSON supports the following data structures (Java equivalents): Scalar, Array/List, and Map.

A Set is not supported out-of-the-box in JSON.

I thought about several ways to represent a set in JSON:

[1] - As a list

However, a list has its own ordering, so the following two lists, ["a", "b"] and ["b", "a"] are not equal as lists, but they should be equal as sets.

[2] - As a map

Use the key-set of the map, and ignore the values.

But again, using standard comparison, the two are not the same as maps:

{"a": "foo", "b": "bar"}, {"a": null, "b": null}

[3] - As a map, with a special value

Take a scalar, say 0 or null and force it to be the value of every key in the map:

{"a": 0, "b": 0}

This way, under standard comparison tools, the objects are equal, even if the key ordering is changed.

However, this technique pollutes the JSON document with irrelevant data.

[4] - As an ordered list

Back to the first suggestion, but this time as an ordered list. This kind of solves the comparison issue.

However, we should also put in mind the complexity of sorting, and also that map notation handles duplicates, while a sorted list does not. Example:

{"a": 400, "a": 9} is handled as {"a": 9}, but ["g", "g"] would always be ["g", "g"].

Having said all that, it seems to me that the list notation is clearer, but the map notation is more robust to keys duplication, and make it harder to be consistent about the special value (even though null seems like a good choice for that).

What do you think? How would you represent a set in JSON?

P.S.

Note that question this is merely about JSON. I know that other formats, like yaml, are available. Still...

6
  • 1
    Sets aren't supported by JSON, it's outside the scope. A set, or a distinct, unique collection, exists within the scope of the application. Because it's a collection, it would be more obvious to use the collection syntax.
    – Zymus
    Commented Aug 6, 2017 at 5:44
  • 1
    Why would you want to represent sets in JSON? Remember JSON is an exchange format.
    – Andres F.
    Commented Aug 6, 2017 at 19:28
  • @AndresF. I thought it would be a nice idea to express the uniqueness attribute of the values. I wouldn't tie JSON just to the exchange format. It can also be useful for document storage (like in MongoDB).
    – Ron Klein
    Commented Aug 6, 2017 at 19:32
  • @RonKlein Fair enough. But ugh... don't get me started with MongoDB :P
    – Andres F.
    Commented Aug 6, 2017 at 19:35
  • In YAML, sets are represented as your option [3], but it has special notation that JSON doesn't have.
    – Jasmijn
    Commented Aug 7, 2017 at 7:12

2 Answers 2

37

Well, you can't. As you said, you can represent arrays and dictionaries. You have two choices.

Represent the set as an array. Advantage: Converting from set to array and back is usually easy. Disadvantage: An array has an implied order, which a set doesn't, so converting identical sets to JSON arrays can create arrays that would be considered different. There is no way to enforce that array elements are unique, so a JSON array might not contain a valid set (obviously you could just ignore the duplicates; that's what is likely to happen anyway).

Represent the set as a dictionary, with an arbitrary value per key, for example 0 or null. If you just ignore the values, this is a perfect match. On the other hand, you may have no library support for extracting the keys of a dictionary as a set, or for turning a set into a dictionary.

In my programming environment, conversion between set and array is easier (array to set will lose duplicate values, which either shouldn't be there, or would be considered correct), so for that reason I would go with arrays. But that is very much a matter of opinion.

BUT: There is a big fat elephant in the room that hasn't been mentioned. The keys in a JSON dictionary can only be strings. If your set isn't a set of strings, then you only have the choice of using an array.

2
  • 9
    the edge case of non-strings is a good argument against a dictionary.
    – Ron Klein
    Commented Aug 6, 2017 at 19:35
  • or you could put values as the dictionary values using random keys, avoiding the issue.
    – Sushi2all
    Commented Aug 23, 2021 at 7:35
10

Don't try to represent sets in JSON. Do it when parsing the data instead.

Your JSON data should have a schema which specifies which fields should be treated as a set, or you may have a metadata embedded in the JSON data itself that describes when a list should be treated as a set (e.g. {"houses": {"_type": "set", "value": [...]}}) or with a naming convention.

Note that according to JSON standard, a JSON object can have duplicate keys. ECMA-404 wordings:

Objects

[...] The JSON syntax does not impose any restrictions on the strings used as names, does not require that name strings be unique, and does not assign any significance to the ordering of name/value pairs. These are all semantic considerations that may be defined by JSON processors or in specifications defining specific uses of JSON for data interchange.

AFAICD, nothing in the spec forbids non unique names, and there are many JSON parser implementations that can parse non unique object names. RFC 7159 discourages non unique names for interoperability, but specifically don't forbid it either, and goes on to list how various parsers have been seen handling non unique object names.

And ECMA 404 also doesn't require that array ordering be preserved:

Arrays

The JSON syntax does not define any specific meaning to the ordering of the values. However, the JSON array structure is often used in situations where there is some semantics to the ordering.

This wording allows applications to use arrays to represent sets if they so choose.

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.