I have to parse JSON objects which contains two keys which can be different in each object. In the example below it is the "path/to/file"
and the accession "ecs2345.ms067"
.
I need to inspect both objects and evaluate the value of "ok"
and "version"
.
All examples I found expect a defined key.
Parser is Python or Postgres jsonb. I am particularly interested if it is possible to use JSON path.
Thanks!
{
"path/to/file": [
{
"ecs2345.ms067": {
"error_type": "__prevalidation__",
"errors": [
"missing : origin_sample_ontology_curie"
],
"ok": false,
"version": "2.0"
}
},
{
"ecs2345.ms067": {
"errors": [],
"ok": true,
"version": "1.0"
}
}
]
}
Use json.loads
to convert the whole thing to a dictionary and then iterate through the dictionary:
json_dict = json.loads(json_string)
for filename, file_data in json_dict.items():
for obj in file_data:
ok = obj["ok"]
version = obj["version"]
# do stuff with the variables.
# if you need the file name, it is filename variable.