Problem : Verify JSON Object validity
Statement
A JSON object is a key-value pair data structure that is enclosed within { }. A sample JSON object would look like
{
"key1":"value1",
"key2":"value2",
"key3": {
"key4":"value4",
"key5":"value5"}
"key6":"value6",
"key7":[
{
"key8":"value8"
}]
}
Given a JSON object, ignore the literal values of the object and check whether the distinguishing characters and notation of the object are valid to determine if the JSON object is valid or not.
Note:
A JSON object is a key-value pair data structure that is enclosed within { }. A sample JSON object would look like
{
"key1":"value1",
"key2":"value2",
"key3": {
"key4":"value4",
"key5":"value5"}
"key6":"value6",
"key7":[
{
"key8":"value8"
}]
}
Given a JSON object, ignore the literal values of the object and check whether the distinguishing characters and notation of the object are valid to determine if the JSON object is valid or not.
Note:
- Key3 points to another JSON object (Concept of nesting of JSON objects).
- Key7 points to an array of JSON objects.
Input Format:
- First line contains a pattern of JSON without any literal
Output Format:
Print 1 if pattern is valid, -1 otherwise.
Print 1 if pattern is valid, -1 otherwise.
Constraints:
- A JSON object should start with '{' and ends with a '}'.
- The key and value should be separated by a ':'.
- A ',' suggests an additional JSON property.
- An array only consists of JSON objects. It cannot contain a "key":"value" pair by itself.
Example 1:
Input
{:[{},{}]}
Output
1
Explanation
{
"Key": [{
"Key": "Value"
}, {
"Key": "Value"
}]
}
Pattern is following all constraints hence prints 1
Input
{:[{},{}]}
Output
1
Explanation
{
"Key": [{
"Key": "Value"
}, {
"Key": "Value"
}]
}
Pattern is following all constraints hence prints 1
Example 2:
Input
{:{[]},{}}
Output
-1
Explanation
Convert this pattern in a JSON Object
{
"Key": {
[
"Key": "Value"
]
},
{
"Key": "Value"
}
}
Constraint 4 "An array only consists of JSON objects. It cannot contain a "key":"value" pair by itself." not followed here, so it's a invalid pattern, hence prints -1
Input
{:{[]},{}}
Output
-1
Explanation
Convert this pattern in a JSON Object
{
"Key": {
[
"Key": "Value"
]
},
{
"Key": "Value"
}
}
Constraint 4 "An array only consists of JSON objects. It cannot contain a "key":"value" pair by itself." not followed here, so it's a invalid pattern, hence prints -1
No comments :
Post a Comment