'Comparing two variables in JSON File Python

Quick question - I have a Python Application that retrieves data from an API and implements it within a json file, and I programmed that functionality well. Now, I need to read the data, and if two variables in the .json file are greater than or less than, I need to recall the web url and recall the web request and add to the .json file.

So I guess I need to convert elements in the json file to Python objects, and then do an operation?

How would I go about doing this in Python?



Solution 1:[1]

import json

with open("file.json", "r") as f:
    json_obj = json.loads(f.read())

# compare values in 'json_obj' here

Example 1:

if json_obj["key"] > 3.14:
    do_something()

Example 2:

if json_obj["key"]["sub_key"] > 3.14:
    do_something()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1