#K66632. JSON Path Value Extraction
JSON Path Value Extraction
JSON Path Value Extraction
Your task is to extract values from a JSON object using dot-separated paths. Given a valid JSON string and a list of paths, you are required to traverse the JSON structure according to each path and output a JSON array of the resulting values. If a path does not exist, output null for that entry.
For example, for the path , you should first access the key a
, then b
(which can be an object or array), and finally the element at index 0 if the current value is an array. Handle errors gracefully if any part of the path is missing.
Note: The extraction is defined recursively and follows the formula: $$value = f(JSON, path)$$ where f is the extraction function.
inputFormat
The input is provided via standard input (stdin) and consists of multiple lines:
- The first line contains a valid JSON string.
- The second line contains an integer (N), the number of paths to be processed.
- Each of the next (N) lines contains a dot-separated path string.
outputFormat
Output a single line to standard output (stdout) containing a JSON array of values corresponding to the given paths. If a value cannot be found, output null in its place.## sample
{"user": {"name": "John", "age": 30, "address": {"city": "New York", "zipcode": "10001"}}, "posts": [{"id": 1, "title": "Hello World"}]}
6
user.name
user.age
user.address.city
user.address.zipcode
posts.0.title
posts.1.title
["John",30,"New York","10001","Hello World",null]