Saturday, July 14, 2018

TCS CodeVita Problem : Verify JSON Object validity C-language Program

Problem : Verify JSON Object validity

www.matterhere.com - Nareddula Rajeev Reddy (NRR)


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:
  1. Key3 points to another JSON object (Concept of nesting of JSON objects).
  2. Key7 points to an array of JSON objects.
You may wish to refer site1 to get a more formal description of JSON grammar. site2,site3; are also good resources to understand JSON specifications. 

Input Format:
  1. First line contains a pattern of JSON without any literal

Output Format:

Print 1 if pattern is valid, -1 otherwise.
Constraints:
  1. A JSON object should start with '{' and ends with a '}'.
  2. The key and value should be separated by a ':'.
  3. A ',' suggests an additional JSON property.
  4. 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
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


TCS CodeVita 2016 Round2 Question: Verify JSON Object validity
C-Language Program (MockVita2/jsonvalidity.c)

#include <stdio.h>
#include <stdlib.h>

#define EXPECT(x)               \
    if (ch != x)                \
    {                           \
        printf("-1");         \
        /*printf("%d", __LINE__);*/\
        exit(0);                \
    }

#define SCAN()                                      \
    if (scanf("%c", &ch))                           \
    {                                               \
        /*printf("Scanned %c at %d\n", ch, __LINE__);*/ \
    }

void parseObject();
void parseKeyValue();
void parseArray();

char ch;

int main()
{
    parseObject();
    printf("1");

    return 0;
}

void parseObject()
{
    SCAN();
    EXPECT('{');
    SCAN();
    if (ch != '}')
    {
        ungetc(ch, stdin);
        parseKeyValue();
    }
    EXPECT('}');
}

void parseKeyValue()
{
    SCAN();
    EXPECT(':');
    SCAN();
    if (ch == ',')
        return parseKeyValue();
    if (ch == '[')
    {
        ungetc(ch, stdin);
        return parseArray();
    }
    if (ch == '{')
    {
        ungetc(ch, stdin);
        return parseObject();
    }

    printf("-1");
    exit(0);
}

void parseArray()
{
    SCAN();
    EXPECT('[');
    SCAN();
    if (ch == ']')
        return;

    ungetc(ch, stdin);
    ch = ',';

    while (ch == ',')
    {
        parseObject();
        SCAN();
    }
    EXPECT(']');
    SCAN();
}

Sources:
https://www.programminggeek.in/
https://github.com/

* Ask us, what you want?
EmoticonEmoticon