detect key conflict in a JSON file

J

Jabba Laci

Hi,

How can you detect if a key is duplicated in a JSON file? Example:

{
"something": [...],
...
"something": [...]
}

I have a growing JSON file that I edit manually and it might happen
that I repeat a key. If this happens, I would like to get notified.
Currently the value of the second key silently overwrites the value of
the first.

Do you know about a command line JSON validator?

Thanks,

Laszlo
 
R

Roy Smith

Jabba Laci said:
I have a growing JSON file that I edit manually and it might happen
that I repeat a key. If this happens, I would like to get notified.

The real answer here is that JSON is probably not the best choice for
large files that get hand-edited. For data that you intend to hand-edit
a lot, YAML might be a better choice.
Currently the value of the second key silently overwrites the value of
the first.

Yeah, that's what I would expect. http://www.json.org/ is mute on the
question of what to do with duplicate keys, but I would be quite
surprised to discover any implementation which behaved differently.
Do you know about a command line JSON validator?

All JSON implementations validate their input. You are assuming that
having duplicate keys is "invalid". I would think it falls more into
the category of "undefined behavior".
 
J

Jabba Laci

The real answer here is that JSON is probably not the best choice for
large files that get hand-edited. For data that you intend to hand-edit
a lot, YAML might be a better choice.

Thanks but how would it be different with YAML? Can YAML check duplicate keys?

How to process (read) YAML files in Python? Can you give me some hints
how to get started? All I need is read it in and create a dictionary
of it.

Thanks,

Laszlo
 
E

Ervin Hegedüs

Hello,

Thanks but how would it be different with YAML? Can YAML check duplicate keys?

no, I think it can't,

I'm using yaml a few months ago, as I noticed, the last key
prevail.

How to process (read) YAML files in Python? Can you give me some hints
how to get started? All I need is read it in and create a dictionary
of it.


import yaml


struct = yaml.load(file("yamlfile.yml" 'r'))



and struct will be a Python dictionary,



As I know, yaml modul is not part of standard Python library, but
most Linux systems has package, eg. Debian/Ubuntu has a
"python-yaml".



Cheers:


a.
 
R

rusi

Thanks but how would it be different with YAML? Can YAML check duplicate keys?

How to process (read) YAML files in Python? Can you give me some hints
how to get started? All I need is read it in and create a dictionary
of it.

Thanks,

Laszlo

There seems to be a suggested patch for duplicate-detection here
http://pyyaml.org/ticket/128
 
R

Roy Smith

Thanks but how would it be different with YAML? Can YAML check duplicate keys?

Oh, I didn't mean to imply that. I was just pointing out that in general, YAML is more friendly for hand-editing than JSON. They both have the same GIGO issue.
How to process (read) YAML files in Python?

Take a look at http://pyyaml.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top