Answers for "json structure check"

1

json structure check

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
Posted by: Guest on May-25-2021
0

how to verify json format is valid

I use Gson class in order to convert a
Json object into a java object.
If it works without any error, it means that it is a valid json format...

import com.google.gson.Gson;

public class JSONUtils {

  Gson gson = new Gson();

  public boolean isJSONValid(String jsonInString) {
      try {
          gson.fromJson(jsonInString, Object.class);   
          return true;
      } catch(com.google.gson.JsonSyntaxException e) { 
          return false;
      }
  }
}
Posted by: Guest on January-14-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language