Flatten Nested JSON Structure
PYTHON coding challenge · Difficulty: easy · Topic: JSON Manipulation · +50 XP
Problem
Given a nested JSON object, write a function to flatten it into a single-level dictionary with keys as the path to each value in the JSON. This operation is common when normalizing data for machine learning models or simplifying API payloads.
Example Input
The data below is already defined — do not redefine it.
`python
nested_json = {
"name": "John",
"address": {
"street": "123 Elm St",
"city": "Springfield"
},
"phoneNumbers": {
"home": "123-456-7890",
"mobile": "098-765-4321"
}
}
`
Expected Output
`
{"name": "John", "address.street": "123 Elm St", "address.city": "Springfield", "phoneNumbers.home": "123-456-7890", "phoneNumbers.mobile": "098-765-4321"}
`
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
“Flatten Nested JSON Structure” is a easy-level PYTHON challenge focused on JSON Manipulation. Working through it gives you hands-on practice with dictionary, recursion, json — 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
- dictionary
- recursion
- json
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 ≤ number of keys in nested_json ≤ 1,000
- Keys and values are strings
- Return a flat dictionary with keys as paths to values
- Use recursion to handle arbitrarily nested structures
- Expected time complexity: O(N), where N is the total number of keys and sub-keys
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
- Top K Frequent Elements in a Stream
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 easy and covers JSON Manipulation.