Find the Depth of a JSON

A JSON object is a popular data format with diverse uses in data storage and communication. It is simple and easy to read, and it can be parsed and generated by a wide multitude of languages. The format is essentially an unordered collection of key:value pairs (like a dictionary). key:value pairs come in the form:
"key": value
With both "key" and value being any valid JSON data type. These pairs are separated by commas and surrounded by braces { } to form an object. Values can be another JSON object, increasing the depth of the JSON object.
Your task is to create a Python function calculate_depth that receives a JSON object as an argument and returns the maximum depth of the object.
The JSON object will be presented as a string. For simplicity, you can assume that the JSON object will be well-formatted and all keys will be strings. However, the values can be strings, numbers, boolean, null, or other JSON objects (represented as nested dictionaries).
How to convert a JSON string to a dictionary in Python?
You can use the json module in Python to convert a string representation of a JSON object into a Python dictionary:
import json

json_string = '{"name": "John", "age": 30, "city": "New York"}'

# Use json.loads() to convert the string to a dictionary
dict_obj = json.loads(json_string)

# Output: {'name': 'John', 'age': 30, 'city': 'New York'}
In this example, json.loads(json_string) is used to convert the json_string into a Python dictionary. The loads function is short for "load string". You can then use this dictionary in your Python program just like any other dictionary.
Your function should return a single integer - the maximum depth of the input JSON object. The depth is defined as the maximum level of nested JSON objects. The top-level JSON object has a depth of 1, a JSON object within another JSON object has a depth of 2, and so on.
{ "name":"John", "age":30, "city":"New York"}
{ "name":"John", "age":30, "city": { "name": "New York", "population": 8000000 }}
{ "name":"John", "details": { "age":30, "city": { "name": "New York", "population": { "year_2023": 8000000, "year_2022": 7900000 }}}}
Note: Depth is not defined by the number of key:value pairs but by the maximum level of nested JSON objects.


Time limit: 2.4 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in