Validate JSON Array String

Difficulty: Easy · Topics: JSON, Validation · Acceptance: 33.3% of 15 submissions

Difficulty: Easy  |  Tags: JSON, Validation

Description

Given a string str, determine if it represents a valid JSON array.

Implement:

isValidJSONArray(str) → boolean

Examples

Example 1:

Input:  str = "[1, 2, 3]"
Output: true

Example 2:

Input:  str = "not an array"
Output: false

Example 3:

Input:  str = null
Output: false

Example 4:

Input:  str = "[{\"name\": \"Alice\"}, {\"name\": \"Bob\"}]"
Output: true

Example 5:

Input:  str = "{\"key\": \"value\"}"
Output: false
Explanation: Valid JSON, but it's an object, not an array.

Constraints

Hints

Hint 1

Try parsing it as a JSON array. If parsing throws, it's invalid.

Practice this problem

Related problems