1
Line_Of_Business = {
        LOB1           : "LOBAppType=sadfsd",  
        LOB2           : "LOBAppType=asdf",
        LOB3           : "LOBAppType=asdf}
        LOB4           : "LOBAppType=sdf",
};

Market = {
        MKTID001       :"MarketName=US||Descr=USA",
        MKTID002       "MarketName=JP||Descr=Japan""
            LOB3           : "LOBAppType=asdf",
        LOB4           : "LOBAppType=sdf",

in this example Market and Line of Business are two json object. Is there any way of validating the syntax of the json object.

In Market json object "MKTID002" key and in Line of business "LOB3" key the syntax is wrong and the Market object is not closed properly.

Is there any way of finding the find the error using java

Jean
  • 123
  • 4
  • 1
    In JSON the keys are strings, and therefore **have** to be contained within double quotes, making none of your key-value pairs valid. In terms of validation, what are you looking for: To be told precisely what the issue is or simply to identify that whatever has been entered isn't valid JSON? – Anthony Grist Dec 04 '12 at 10:43
  • there are several libraries which validates the format apart from providing parsing/constructing json messages, You can check here: http://www.json.org/ – vishal_aim Dec 04 '12 at 10:44
  • Duplicate? http://stackoverflow.com/questions/2499126/json-schema-validation-using-java – Udo Klimaschewski Dec 04 '12 at 10:44
  • http://www.jsonlint.com/ - 'nuff said! – Henrik Andersson Dec 04 '12 at 10:53
  • they are valid json entity which are stored in a js file so is there any way to validate them. Because i have used this site www.freeformatter.com to validate and it was able to validate it.So is there any API which could be used in this scenario – Jean Dec 07 '12 at 07:07

3 Answers3

2

Well the obvious way to validate the basic syntax of some JSON is to simply try to load / read / parse it using one of the many JSON parser libraries. (And if the parse / read /load succeeds, you can throw away the resulting data structure). json.org Lists a number of JSON libraries in a number of languages ... including Java.

If building and throwing away a data structure is a concern, then some of the libraries offer JSON validation separate from loading; e.g. http://www.stringtree.org/stringtree-json.html

And if simply want to do this validation on an ad hoc basis (i.e. not as part of some larger application), then try the jsonlint.com service.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Sample syntax-check with Gson (using lombok):

package com.stackoverflow.so13701022;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;

import lombok.Data;

public class CheckJson {
    @Data
    private static class LineOfBusiness {
        @SerializedName("LOB1")
        private String lob1;

        @SerializedName("LOB2")
        private String lob2;

        @SerializedName("LOB3")
        private String lob3;

        @SerializedName("LOB4")
        private String lob4;
    }

    @Data
    private static class Market {
        @SerializedName("MKTID001")
        private String mktid001;

        @SerializedName("MKTID002")
        private String mktid002;

        @SerializedName("LOB3")
        private String lob3;

        @SerializedName("LOB4")
        private String lob4;
    }

    private static final String ONE = "{" + 
    "        LOB1           : \"LOBAppType=sadfsd\"," + 
    "        LOB2           : \"LOBAppType=asdf\"," + 
    "        LOB3           : \"LOBAppType=asdf}" + 
    "        LOB4           : \"LOBAppType=sdf\"," + 
    "}";

    // I corrected this one on purpose
    private static final String TWO = "{" + 
    "        MKTID001       :\"MarketName=US||Descr=USA\"," + 
    "        MKTID002       :\"MarketName=JP||Descr=Japan\"," + 
    "        LOB3           : \"LOBAppType=asdf\"," +
    "        LOB4           : \"LOBAppType=sdf\"" + 
    "}";

    public static void main(String[] args)
    {
        final Gson gson = new GsonBuilder().create();
        System.out.println(tryParse(gson, ONE, LineOfBusiness.class));
        System.out.println(tryParse(gson, TWO, Market.class));
    }

    private static <T> T tryParse(Gson gson, String what, Class<T> clazz)
    {
        try {
            return gson.fromJson(what, clazz);
        } catch (final JsonSyntaxException e) {
            System.err.println(e);
        }
        return null;
    }
}

Output (Err = stderr, Out = stdout):

(Err) com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 159
(Out) null
(Out) CheckJson.Market(mktid001=MarketName=US||Descr=USA, mktid002=MarketName=JP||Descr=Japan, lob3=LOBAppType=asdf, lob4=LOBAppType=sdf)
  • actually i am loading these objects from a js file. Is there any way to do it dynamically – Jean Dec 05 '12 at 11:22
0

Use any JSON library for java ( org.json.JSONObject) and you will get all features like parsing and validation inbuilt in API.

These libraries take any String as argument and if it fails to parse the string into JSONObject then throws Exception.

 JSONObject jsob = new JSONObject(StringRepresentationOfJson);
rai.skumar
  • 10,309
  • 6
  • 39
  • 55