json corrector code example
Example 1: 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 2: json beautify
fetch('https://fakestoreapi.com/products',{
method:"POST",
body:JSON.stringify(
{
title: 'test product',
price: 13.5,
description: 'lorem ipsum set',
image: 'https://i.pravatar.cc',
category: 'electronic'
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))