Construct JSONPath from Arguments

Difficulty: Medium · Topics: JSON, String Building · Acceptance: 50.0% of 2 submissions

Difficulty: Medium  |  Tags: JSON, String Building

Description

Given a variadic list of arguments (strings and integers), construct a valid JSONPath string.

Rules:

Implement:

constructJSONPath(args...) → String

Examples

Example 1:

Input:  ("store", "book", 0, "title")
Output: "$.store.book[0].title"

Example 2:

Input:  ("users", 2, "name")
Output: "$.users[2].name"

Example 3:

Input:  (null, "data", 0)
Output: "$.data[0]"

Example 4:

Input:  ("items", -1, "value")
Output: "$.items.value"
Explanation: Negative index -1 is ignored.

Constraints

Hints

Hint 1

Iterate through args: append . before strings, append [n] for non-negative integers.

Practice this problem

Related problems