Copyassignment.com



JSON WITH PYTHON The JSON stands for JavaScript Object Notation. The JSON is a standard format that is used for storing and exchanging of datas. It has datas stored in the form of key-value pair. Using python, we can generate and store datas in json files. The json files are stored with the extension .json .Getting started:To use json files or to perform any operation with json involved in it, you need to import the json i.e.import jsonYou can also import the requests module along with the json module. The requests module is used to extract the contents from the browser. In this case if the json file is heavy weighted.import requestsParsing json:The loads() methods is used to parse the json in python. Once the json file is parsed, it is stored in the form of dictionary. Example:import jsonjson_data = '{"name":"Jenny","age":25,"profession":"developer"} 'dic_data = json.loads(json_data)print(dic_data[“age”])Output:25Converting to Json:To convert the datas into json format in python, we use the dumps () method. To convert into json, the datas must be in json strings.Example:import jsondic_data = '{"name":"Jenny","age":25,"profession":"developer"} ' json_data = json.dumps(dic_data)print(json_data)Output:‘{“name” : “Jenny” , “age”:”25” , “profession” : “developer”}’JSON Strings :The python types that can be converted into json strings are:Dict – the dictionary is converted as a json object i.e. the dictionary itself is in the form of jsonList – the list is stored in the form of arrays in jsonTuple – similar to list, tuples are also stored in the form of arrays in jsonString – stored as string similar to that in pythonInt – stored as a number in jsonFloat – stored as a number in jsonBoolean – similar to that in python .i.e. true or falseNone – stored as null value in jsonThe json file can have nested datas. It can store an array of values for a specific key similar to that of a dictionary in python.Example: import jsondic_data = {"name" : "Jenny","age" : 25,"profession" : "developer","skill set" : ("C" ,"CPP", "JAVA", "PYTHON", "DOCKER", "JAVASCRIPT", "ANGULAR", "NODE", "REACT", "SQL"),"previous experience" : None,"academic details" : [{"qualification" : "UG" , "score" : 7.5 },{"qualification" : "HSC" , "score" : 90},{"qualification" : "SSC" , "score" : 9.6}]}json_data = json.dumps(dic_data)print(json_data)Output:{"name": "Jenny", "age": 25, "profession": "developer", "skill set": ["C", "CPP", "JAVA", "PYTHON", "DOCKER", "JAVASCRIPT", "ANGULAR", "NODE", "REACT", "SQL"], "previous experience": null, "academic details": [{"qualification": "UG", "score": 7.5}, {"qualification": "HSC", "score": 90}, {"qualification": "SSC", "score": 9.6}]}Formatting the json appearance:Indentation:The output of the json may be looking congested. To improve the readability, the indents can be used inside the dumps() method. Syntax:json.dumps(x, indent =value)where,x – is the object which has the json datas stored in the form of dictionaryindent – used to improve the readabilityvalue – the higher the value, the more indentations and spaces are created between each key- value pairs of data.Example :import jsondic_data = {"name" : "Jenny","age" : 25,"profession" : "developer","skill set" : ("C" ,"CPP", "JAVA", "PYTHON", "DOCKER", "JAVASCRIPT", "ANGULAR", "NODE", "REACT", "SQL"),"previous experience" : None,"academic details" : [{"qualification" : "UG" , "score" : 7.5 },{"qualification" : "HSC" , "score" : 90},{"qualification" : "SSC" , "score" : 9.6}]}print(json.dumps(dic_data, indent=4))Output:{ "name": "Jenny", "age": 25, "profession": "developer", "skill set": [ "C", "CPP", "JAVA", "PYTHON", "DOCKER", "JAVASCRIPT", "ANGULAR", "NODE", "REACT", "SQL" ], "previous experience": null, "academic details": [ { "qualification": "UG", "score": 7.5 }, { "qualification": "HSC", "score": 90 }, { "qualification": "SSC", "score": 9.6 } ]}Usage of separators:By default the separators used are (, and :), but instead of them we can specify and change them to (. and =). To achieve that we use separators(), specify the change and pass it inside the dumps() method. Syntax: json.dumps(x, indent =value, separators(“.” , “=” ))where,x – is the object which has the json datas stored in the form of dictionaryindent – used to improve the readabilityvalue – the higher the value, the more indentations and spaces are created between each key- value pairs of data.separators( “.” ,”=”) – the separators that need to be used instead of the default ones. Always we need to pass the separators inside the quotes.Example:import jsondic_data = {"name" : "Jenny","age" : 25,"profession" : "developer","skill set" : ("C" ,"CPP", "JAVA", "PYTHON", "DOCKER", "JAVASCRIPT", "ANGULAR", "NODE", "REACT", "SQL"),"previous experience" : None,"academic details" : [{"qualification" : "UG" , "score" : 7.5 },{"qualification" : "HSC" , "score" : 90},{"qualification" : "SSC" , "score" : 9.6}]}print(json.dumps(dic_data, indent=3,separators=(". ", " = ")))Output:{ "name" = "Jenny". "age" = 25. "profession" = "developer". "skill set" = [ "C". "CPP". "JAVA". "PYTHON". "DOCKER". "JAVASCRIPT". "ANGULAR". "NODE". "REACT". "SQL" ]. "previous experience" = null. "academic details" = [ { "qualification" = "UG". "score" = 7.5 }. { "qualification" = "HSC". "score" = 90 }. { "qualification" = "SSC". "score" = 9.6 } ]}Sorting the resulting data:To sort the resultant data, use sort_keys parameter in the dumps() method. The sort_keys value can be True or False based on the requirement of the user.Syntax: json.dumps(x, indent =value, sort_keys=bvalue)where,x – is the object which has the json datas stored in the form of dictionaryindent – used to improve the readabilityvalue – the higher the value, the more indentations and spaces are created between each key- value pairs of data.sort_keys – parameter to indicate whether to sort the resultant data or notbvalue – the value for sort_keys. It can be of Boolean type .i.e either True or FalseExample:import jsondic_data = {"name" : "Jenny","age" : 25,"profession" : "developer","skill set" : ("C" ,"CPP", "JAVA", "PYTHON", "DOCKER", "JAVASCRIPT", "ANGULAR", "NODE", "REACT", "SQL"),"previous experience" : None,"academic details" : [{"qualification" : "UG" , "score" : 7.5 },{"qualification" : "HSC" , "score" : 90},{"qualification" : "SSC" , "score" : 9.6}]}print(json.dumps(dic_data, indent=3, sort_keys=True))Output:{ "academic details": [ { "qualification": "UG", "score": 7.5 }, { "qualification": "HSC", "score": 90 }, { "qualification": "SSC", "score": 9.6 } ], "age": 25, "name": "Jenny", "previous experience": null, "profession": "developer", "skill set": [ "C", "CPP", "JAVA", "PYTHON", "DOCKER", "JAVASCRIPT", "ANGULAR", "NODE", "REACT", "SQL" ]} ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download