Complex JSON Transformation for Nested Metrics
PYTHON coding challenge · Difficulty: hard · Topic: JSON Processing · +200 XP
Problem
In data engineering tasks, we often extract, transform, and load nested JSON data. This challenge requires transforming nested JSON data into a flattened dictionary containing specific metrics. Given a JSON object representing nested hierarchical data, write a function to extract and calculate specific metrics at various levels, such as average values and counts, and return a flattened dictionary with these metrics.
Example Input
The data below is already defined — do not redefine it.
`python
sample_data = {
"level1": {
"level2": {
"values": [10, 20, 30],
"metrics": {
"count": 3,
"sum": 60
}
},
"level2_other": {
"values": [5, 15],
"metrics": {
"count": 2,
"sum": 20
}
}
}
}
`
Expected Output
`
{"level1.level2.average": 20.0, "level1.level2_other.average": 10.0, "level1.total_count": 5, "level1.total_sum": 80}
`
Notes
- Print the result — the grader reads standard output
- Do not redefine the input; it is provided for you
What this PYTHON challenge teaches you
“Complex JSON Transformation for Nested Metrics” is a hard-level PYTHON challenge focused on JSON Processing. Working through it gives you hands-on practice with json, recursion, dictionary, hierarchical data — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- json
- recursion
- dictionary
- hierarchical data
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- 1 ≤ depth of JSON hierarchy ≤ 10
- The total number of values across all levels ≤ 100,000
- Each 'values' list contains integers in range [-10^9, 10^9]
- Metrics contain 'count' and 'sum' fields that are consistent with 'values'
- Expected time complexity is O(N), where N is the total number of values
How to practise it on PySpark.in
Open the challenge, write your Python code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related PYTHON challenges
- Word Frequency Counter
- Validate Unique User IDs
- Extract Error Codes from Log Strings
- Sum of Values by Key
- Detect Anomalies in Transaction Batches
- Flatten Nested JSON Structure
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYTHON environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated hard and covers JSON Processing.