0

I want to ensure that the same key is not added to a json object more than once. The key itself could be in the parent/root nodes or could be in any of the children.

Is there a way to force this?

  • "The key itself could be in the parent/root nodes or could be in any of the children."—JSON is a very flexible data structure. What are you actually trying to do here? You may have an [XY problem](https://meta.stackexchange.com/q/66377/248627). – ChrisGPT was on strike Mar 02 '20 at 01:09
  • 1
    Check all the keys before adding a new key. – Pedro Lobito Mar 02 '20 at 01:39
  • @HeapOverflow, I wrote that before seeing the "in any parent / child node" piece (which is a weird requirement—leading to my next comment). Naturally, JSON only permits a single same key per object. – ChrisGPT was on strike Mar 02 '20 at 01:40
  • @Chris Where do you get that from? Are the [answers here](https://stackoverflow.com/q/21832701/12671057) wrong? – Kelly Bundy Mar 02 '20 at 01:44
  • @HeapOverflow, practically, yes. `{"foo": "bar", "foo": "baz"}` is _valid_, but all implementations I know will spit out `{"foo": "baz"}` from this literal. Certainly with Python's standard `json` module (Python is tagged), assuming `dict` as a native data structure to represent JSON objects (its behavior when decoding and a valid input for encoding), that is what will happen. And since Python `dict`s and JavaScript objects are indexed by key, this makes sense. – ChrisGPT was on strike Mar 02 '20 at 02:06
  • @Chris That doesn't sound like "JSON itself", though. – Kelly Bundy Mar 02 '20 at 02:09
  • @HeapOverflow, you really like to argue. JSON comes directly from JavaScript. I invite you to show any real JavaScript implementation that permits multiple same keys in an object persistently. Or any JSON implementation, for any real programming language, that does so. I'd be interested in seeing its interface. Every one I've ever seen maps to something like Python's `dict`. – ChrisGPT was on strike Mar 02 '20 at 02:12
  • @Chris Again that's not JSON itself but "implementations". And Python supports `json.loads('{"a": 1, "a": 2}', object_pairs_hook=print)`, printing `[('a', 1), ('a', 2)]` without complaining. – Kelly Bundy Mar 02 '20 at 02:33
  • @HeapOverflow, I'm going to assume you're right about that. It's not very practical, is it? Is there a native Python data structure that maps nicely to that, aside from a list of tuples? Do you think OP is really asking about what JSON theoretically permits, rather than what it practically allows? Feel free to add an _answer_ instead of arguing uselessly in the comments. You seem to want me to be wrong more than to help OP. – ChrisGPT was on strike Mar 02 '20 at 02:37
  • Interesting, so it seems that JSON doesnt normally allow duplicates, but the python implementation doesnt complain. This is true as per my testing so far. – Data Science Enthusiast Mar 02 '20 at 04:37
  • d2 = [ { "key" : 1, "value" : "something" }, { "key" : 2, "value" : "something else" }, { "key" : 1, "value" : "something agian" } ] print(json.dumps(d2, indent=4)) – Data Science Enthusiast Mar 02 '20 at 04:37
  • 1
    @DataScienceEnthusiast, Heap Overflow did you a disservice by muddying the waters, which is the opposite of what SO should do. In all practical senses, JSON _objects_ can only have a single value for any given key. What you show in your last comment is a `list` of `dict`s, which becomes an _array of_ objects, not a single object. Separate objects can have the same key. – ChrisGPT was on strike Mar 02 '20 at 13:31
  • @DataScienceEnthusiast, would you please answer my top comment about what you're trying to actually do? What do unique keys throughout an entire nested data structure achieve? There may be a good solution, but it's hard to know without more context. – ChrisGPT was on strike Mar 02 '20 at 13:33
  • @chris i am currently trying to convert a flat file directory listing (example can be seen here) into a json object so that i can store it as json structured type. As I have no way to ensure that the same directory is not listed multiple times i want to ensure the key (which is the folder path) is only added to the json object once as a "parent" directory. Hope this helps. And sorry for taking long to respond – Data Science Enthusiast Mar 03 '20 at 05:49
  • JSON objects hold key / value pairs. If a directory path is used as a key, what is its value? Another object showing its contents? Do `null` values indicate files? [Edit]ing an example into your question might be helpful. – ChrisGPT was on strike Mar 03 '20 at 16:35

0 Answers0