#K82722. Retrieve Nested JSON Value

    ID: 36039 Type: Default 1000ms 256MiB

Retrieve Nested JSON Value

Retrieve Nested JSON Value

You are given a JSON object that may contain nested objects and arrays, and a query string that specifies a path in the object. Your task is to retrieve the value corresponding to the given query.

The query string always starts with a dot (.) and each subsequent field is separated by a dot. When traversing the JSON object:

  • If at any point the required key does not exist, output no such property.
  • If the final value is an object (i.e. a nested JSON object), output object.
  • If the final value is an array, output array.
  • Otherwise, output the string representation of the value.

Note: The solution should read input from standard input and write the result to standard output.

It is guaranteed that the input JSON and query will be provided in a valid format.

inputFormat

The input consists of two lines:

  1. The first line contains a JSON object.
  2. The second line contains the query string.

Both inputs are provided via standard input (stdin).

outputFormat

Print a single line to standard output (stdout) which is the result of retrieving the value from the JSON object as per the query string.

  • If the key does not exist at any point, output no such property.
  • If the resulting value is a nested object, output object.
  • If the resulting value is an array, output array.
  • Otherwise, output the string representation of the value.
## sample
{"employee": {"details": {"name": "John Doe", "age": 30, "address": {"street": "123 Maple Street", "city": "Springfield"}, "roles": ["engineer", "manager"]}, "id": "E12345"}}
.employee.details.name
John Doe