valid json string code example
Example 1: javascript is valid json string
function isValidJSONString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
var personJSONString = '{"first_name":"Tony","last_name":"Hawk","age":31}';
if(isValidJSONString(personJSONString)){
var person= JSON.parse(personJSONString);
}
Example 2: 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;
}
}
}
Example 3: JSON validate
import numpy as np
df['first_five_Letter']=df['Country (region)'].str.extract(r'(^w{5})')
df.head()