1import json
2
3studentJson ="""{
4 "id": 1,
5 "name": "john wick",
6 "class": 8,
7 "percentage": 75,
8 "email": "jhon@pynative.com"
9}"""
10
11print("Checking if percentage key exists in JSON")
12student = json.loads(studentJson)
13if "percentage" in student:
14 print("Key exist in JSON data")
15 print(student["name"], "marks is: ", student["percentage"])
16else:
17 print("Key doesn't exist in JSON data")
1I use Gson class in order to convert a
2Json object into a java object.
3If it works without any error, it means that it is a valid json format...
4
5import com.google.gson.Gson;
6
7public class JSONUtils {
8
9 Gson gson = new Gson();
10
11 public boolean isJSONValid(String jsonInString) {
12 try {
13 gson.fromJson(jsonInString, Object.class);
14 return true;
15 } catch(com.google.gson.JsonSyntaxException e) {
16 return false;
17 }
18 }
19}