Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Querying Nested JSON with Path
00:00
5 left

Querying Nested JSON with Path

MediumSQL · PostgreSQL

Problem

Given a Json Object in the form of Map<String, Object>, and given a query to match a key by path (similar to jq), extract its value. Asked in the phone screen stage. For example, the path .contacts.cell returns the nested cell value, while a missing path returns null. Reported follow-up: extend the functionality to support any key (*) match.

Write a PostgreSQL query using the stored JSONB object and requested path.

Output

  1. One row per path request with request_id, path_query, and extracted value.
  2. Return null when the object or path does not exist.
  3. Order by request_id ascending.

Schema

json_objects
ColumnTypeDescription
object_idPKINTIdentifier of the stored JSON object
payloadJSONBJSON document to query
path_requests
ColumnTypeDescription
request_idPKINTIdentifier of the path lookup request
object_idINTJSON object requested for lookup
path_queryVARCHAR(255)jq-style path beginning with a dot
Tablesjson_objectspath_requests
Interviewer

Your question is Querying Nested JSON with Path. Start with the requirements and the two tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.