Link to home
Start Free TrialLog in
Avatar of srikotesh
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"},"criticality":{"value":"PROD"}}
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

what type of database is it?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srikotesh
srikotesh

ASKER

mysql
In that case I would do what @CEHJ said.
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;
	}

}

Open in new window

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?
sorry, I forgot to change method name like is valid json object
If I gave invalid json it will returns false