0

I've already said, my question is different...question is already asked here, but i want a predefined method in java which checksgiven string is json format or not.

if there is no predefined method then at least tell a code which checks JSON Format or not, without using try catch block..

Thanks in advance...

3

3 Answers 3

3

Use JSONObject's constructor from a String

    try {
        JSONObject o = new JSONObject(yourString);
    } catch (JSONException e) {
        LOGGER.error("No valid json");
    }
Sign up to request clarification or add additional context in comments.

4 Comments

is there any method ? which doesnt use try catch block?
AFAIK this is the better way, you can build a method containing that block to avoid writing several catch's. Would that be acceptable according to your requirements?
thanks for the reply. i just dont wanna use try catch blocks at all. like in javascript we hava a predefined function which validates string is json or not.. so i thought is there anything in java which automatically checks valid json or not.. i want boolean jsIsValid=method(string);
@edsheeran First of all, Java is not JavaScript. Deal with it. And even in JavaScript you are supposed to use try-catch.
0
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;    
/**
         * 
         * @param inputJosn
         * @return
         * @throws IOException 
         * @throws JsonParseException 
         * @throws JsonProcessingException
         */
        private static boolean isJsonValid(String inputJosn) throws JsonParseException, IOException  {
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
            JsonFactory factory = mapper.getFactory();
            JsonParser parser = factory.createParser(inputJosn);
            JsonNode jsonObj = mapper.readTree(parser);
            System.out.println(jsonObj.toString());


            return true;
        }

Comments

0
public boolean isValidJson(String jsonStr) {
    Object json = new JSONTokener(jsonStr)).nextValue();
    if (json instanceof JSONObject || json instanceof JSONArray) {
      return true;
    } else {
      return false;
    }
}

Just pass any string in this function.

3 Comments

var jsonStr doesn't appear in function body
Just fixed the variable naming. Thanks for mentioning
I think this one need some improvement. below example return true e.g. { "name" : mkj123 mk }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.