Bootstrap FreeKB - Python (Scripting) - Determine if JSON key contains a value
Python (Scripting) - Determine if JSON key contains a value

Updated:   |  Python (Scripting) articles

Let's say you have the following JSON.

{ 
 "foo": "Hello World",
 "bar": ""
}

 

There are several built in modules that are part of the Python Standard Library included with your Python installation, such as os (Operating System) and re (Regular Expression) and sys (System).

Here is how you can determine if the JSON contains a certain key.

#!/usr/bin/python3
import json

file = open("/path/to/example.json")

try:
  json = json.load(file)
except Exception as exception:
  print(f"Got the following exception: {exception}")

file.close()

keys = ["foo", "bar"]

for key in keys:
  if len(json[key]) > 0:
    print(f"{key} in example.com contains a value")
  else:
    print(f"{key} in example.com does not contain a value (empty)")

 

And you are parsing the JSON using json.load or using json.loads, perhaps like this.

#!/usr/bin/python3
import json
raw_json = '{ "foo": "Hello World" }'

try:
  parsed_json = json.loads ( raw_json )
except Exception as exception:
  print(f"Got the following exception: {exception}")

print(parsed_json)

 

An if statement can be used to determine if a key equals a certain value.

#!/usr/bin/python3
import json
raw_json = '{ "foo": "Hello World" }'

try:
  parsed_json = json.loads ( raw_json )
except Exception as exception:
  print(f"Got the following exception: {exception}")

if parsed_json['foo'] == "Hello World":
  print("The foo key contains a value of Hello World")
else:
  print("The foo key does NOT contain a value of Hello World")

 

Running this Python script should return the following.

~]$ python example.py
The foo key contains a value of Hello World

 

Let's say you have nested JSON, like this.

{
  "main":
  {
    "department":
    {
      "username": "john.doe"
    }
  }
}

 

Here is how you can determine if the username key equals a value of john.doe.

#!/usr/bin/python3
import json
 
file = open("/path/to/example.json")

try:
  parsed_json = json.load(file)
except Exception as exception:
  print(f"Got the following exception: {exception}")

file.close()

if parsed_json['main']['department']['username'] == "john.doe":
  print("The main.department.username key contains a value of john.doe")
else:
  print("The main.department.username key does NOT contain a value of john.doe")

 

Here is how you can determine if the foo key contains a value.

#!/usr/bin/python3
import json
raw_json = '{ "foo": "Hello World" }'

try:
  parsed_json = json.loads ( raw_json )
except Exception as exception:
  print(f"Got the following exception: {exception}")

string = "World"

if string in parsed_json['foo']:
  print(f"The foo key contains {string}")
else:
  print(f"The foo key does NOT contain {string}")

 

Or like this, using find.

#!/usr/bin/python
import json
raw_json = '{ "foo": "Hello World" }'

try:
  parsed_json = json.loads ( raw_json )
except Exception as exception:
  print(f"Got the following exception: {exception}")

string = "World"

if parsed_json['foo'].find(string) == 0:
  print(f"The foo key contains {string}")
else:
  print(f"The foo key does NOT contain {string}")

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 4831ca in the box below so that we can be sure you are a human.