0

I have a json file and I'm using ijson for the same but for different files the top-level key is different. Is there any way I can find if the key is present or not

    f = open(filename)
    objects = ijson.items(f, "BulkData.item") # If BulkData is a key or not
1

1 Answer 1

1

let's say that you have JSON like:

{
 "a": {
   "b": "c"
  }
}

And your code is:

key = "a.b"

with open("example.json") as f:
  objects = ijson.items(f, key)
  objects_list = list(objects)
  if len(objects_list) == 0:
    result_value = None
  else:
    result_value = objects_list[0]

if result_value is not None:
  print(result_value)
else:
  print(f"here is no {key} key")

if you set a.b key c will be printed

in other case here is no key will be printed

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.