Categories
Linux Mastering Development

How to validate a json to make sure each json object is valid?

I have a json as shown below where I have multiple json objects inside rootLevel. I need to validate my json to make sure each json object should follow this below rules:

  • Value in clientId, procId and machineName in each json object should be unique. If they are not unique then I want to fail it by printing what is not unique.
  • Required fields are: clientId, some_key6, some_key7, some_key8, some_key9, some_key10 and machineName. If they are not present in any json object then I want to fail it as well.

Below is my json. As of now it only has one json object in rootLevel array but in general it has a lot in similar fashion.

{
    "rootLevel": [
        {
            "clientId": 1234,
            "processId": 1,
            "clientName": "Hello",
            "some_key1": 4,
            "some_key2": 9,
            "some_key3": "rcb.com",
            "some_key4": 7,
            "some_key5": 301,
            "some_key6": 78,
            "some_key7": 2,
            "some_key8": 1,
            "some_key9": "USD",
            "some_key10": "en-US",
            "some_key11": 45,
            "some_key12": {
                "SportsId": [
                    "NBA"
                ],
                "Team": [
                    "RCB"
                ]
            },
            "procId": [
                1234
            ],
            "machineName": [
                "shop.hello.com"
            ]
        }
}

Can we do these validations on each json object using shell script or is there anything else suited for this? I have named few keys as some_key# just to make it easier to understand but in general they have different names.

Leave a Reply

Your email address will not be published. Required fields are marked *