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:
- The path always starts with
$. - String arguments become property accessors:
$.store - Integer arguments become array indices:
[0] - If the first argument is
null, skip it. - Negative integer indices are ignored.
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
1 <= args.length <= 50- Arguments are either
String,Integer, ornull. nullis only valid as the first argument (skip it).- Negative integers are silently ignored.
Hints
Hint 1
Iterate through args: append . before strings, append [n] for non-negative integers.