Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

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