Python Forum
Json data validation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Json data validation (/thread-14322.html)



Json data validation - amar - Nov-24-2018

Hi ,

I have json file like below
{
"fieldname" :{ "name" :"str",
                "age" : 13
                "add" : "str"
                "out" :"Y",
                 "result" :"Y",
"nextfield" :{ "req" :"Y",
              "cfy" : "nan",},
"nextfield1": { "req" :"n",
               "source" :"n/a",
                "target" :"n/a"}
}
like there are many json file, jus 1 json im putting here.

here name, age, add , out , result are mandatory keys .. and nextfield and nextfield1 are optional keys have to do validation for these , like chk the mandatory keys present in input file , if not present should throw exeption mandatory keys not present, same like optional .. also have to validate the values of mandatory keys
suppose if i check the type of name (mandatory keys) should print the type of value. likewise i have to do validation of nextfield and nextfield1 , if req key is "Y" then it will go this field n chk if req : N it will throw error msg ,

kindly suggest how i can achieve this ?


RE: Json data validation - Larz60+ - Nov-24-2018

that looks like a pure dictionary, not json

to read a json file:

import json

with open('jsonfile') as fp:
    mydata = json.load(fp)