srikotesh
asked on
how do i insert json value into database
Hi Experts,
how to insert the below json value into db?
how to validate is it valid json or not?
i will get dynamic json values , i mean it is not static it will differ every
time.
can some suggest me how to do
{"location":{"value":""}," category": {"value":" 33"},"crit icality":{ "value":"P ROD"}}
how to insert the below json value into db?
how to validate is it valid json or not?
i will get dynamic json values , i mean it is not static it will differ every
time.
can some suggest me how to do
{"location":{"value":""},"
what type of database is it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
mysql
In that case I would do what @CEHJ said.
ASKER
To validate json object is valid or not below is the program
package com.howtodoinjava.restful;
import org.json.JSONException;
import org.json.JSONObject;
public class ValidJson {
public static void main(String[] args) {
String json = " {\"location\":{\"value\":\"\"},\"category\":{\"value\":\"33\"},\"criticality\":{\"value\":\"PROD\"}}";
System.out.println(isValidJSONStringArray(json));
}
private static boolean isValidJSONStringArray(String requestBody) {
try {
new JSONObject(requestBody);
} catch (JSONException jsonEx) {
return false;
}
return true;
}
}
ASKER
i have taken my json object as string and inserted in db successfully.
That's good though that method name could be more generic - what happens if no array is involved?
ASKER
sorry, I forgot to change method name like is valid json object
If I gave invalid json it will returns false
If I gave invalid json it will returns false