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"}}
LVL 2
srikoteshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
what type of database is it?
0
CEHJCommented:
how to validate is it valid json or not?
With a JSON parser. If it won't parse properly, it's invalid

Use a PreparedStatement to do the insert - it will escape the string correctly
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
mysql
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

gurpsbassiCommented:
In that case I would do what @CEHJ said.
0
srikoteshAuthor Commented:
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

0
srikoteshAuthor Commented:
i have taken my json object as string and inserted in db successfully.
0
CEHJCommented:
That's good though that method name could be more generic - what happens if no array is involved?
0
srikoteshAuthor Commented:
sorry, I forgot to change method name like is valid json object
If I gave invalid json it will returns false
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.