JSON validation shema using Java - java

I have this JSON:
{
"header":
{
"IssuerID": "000141",
"AuthenticationID": "e07020c0d040a050a0808099",
"AuthenticationDateTime": "20151103093035",
"AuthenticationDateTimeGMT": "20151103093035",
"Signature": "MThEMTExQkMzQzM0OUIxQjM5MDc2MjFGMzMyQjhDNTk1OTI0NDNERTg5ODcwQjNFOTc0ODQwNThBNkQxNTgzNTk2N0YzN0I2OTkyMzI1QjY2OENDQjgxRUNERDlGNDFDNzVCMzQ5Njg5NTY4NzkwNUQ5MzBDN0ExOTVGOUY0OUY2QjlCQzlDQkREOEQ3NjEzRkQ2OEYyMDhEQTY2QTkzNUZDM0UzOTI3RDk2OTYwODg4NTkyNzYyQUJCQkJFREZGNzNCNEEyNUU5OTc5OTFFODk2MTQ0Q0Y4Q0RGNzg1M0JBQTM4QkZBQzRFRUY2MTkzM0E4REI3QkQ0MEJBRkU4OTlEOTVDNTkxOTQ0M0IwNjMyMzZDQ0U4MzdBQTQzODU3RkMyOEQ0Rjk2NUMyRkNERUM0NDREQkIzNUM0QUVERDUzRjFBOTk5RTQ4Mjk4MzNERjU3RTQ1QUE2Nzc4MDUyRTdERTdGRTVFRURGRkVGMTlFN0Y2QTAyQTVCNjk3NUU2OUNGRUU3MzRGNDZDOTE0Q0U3NTk5NzdGNDkyOTdFQkRGREIwNDBCNDhBQTkzMzE1QjU0MzI1Njk0MjgxMjVDMzM5OUM3MTExNTg4NENBOTU3QTM4OTU5MTY5OTQ1NkQ2NDNFOUVEMjU5MjRDNzhEMDQxMEJGM0ZEQUVFM0Y1QTU="
},
"body":
{
"TransactionDateTime": "0151103093035",
"TransactionID": " SP020110216",
"IASauthenticationRequestReferenceID": "h0IrqhqBoUpUCiSv17NB0vHvABIUxHGCurJZFzUWdZJz9TxYio",
"VerificationID": "h0IrqhqBoUpUCiSv17NB0vHvABIUxHGCurJZFzUWdZJz9TxYio ",
"Acquirer_id": "000014",
"MerchantName": " Twin Hotel ",
"CurrencyCodeISO": "360",
"Amount": "970000.00",
"CardNumber": " BmaHLuFRg4SaPnaGB07t5fNrHXr/lyOa ",
"AuthenticationResponseCode": "00",
"AuthenticationResponseDetails": "Success",
"ValidityDateTimeExpireGMT": "20151103094035",
"Expiry_Period": "10"
}
}
How can I validate this json? For example, issuer id must be integer and have min and max values and if someone put headers instead of header it can get result: "json not valid".
I'm using java and json.org.simple for parsing.
Can anyone help me?

If you are looking for real schema validation you might want to have a look at http://json-schema.org/. There are validators for most common languages.
Assuming there are classes to represent the above structure you could try to use Java Bean Validation.

In your case, I would use a deserializer like jackson and not a json-schema, since you seem to have integer and dates encoded as string in your json. Thus, in a json-schema you would need to define strange regex. To see how to use jackson and to weigh the pros and cons of each method you can take a look a my blog post here: Three Ways to Validate JSON in Java

Related

Put missing double quotes in a JSON string value in java

I have a json string where all the values need to be surrounded with double quotes. for example (just a sample, it contains many similar fields)
{"Id": "2017",
"Currency": "AUD",
"Date": 2020-06-22,
"InCash": 0.000,
"Dep": "ABC90",
"sumCash": 770.87,
"AnotherDate": 2020-06-21}
to
{"Id": "2017",
"Currency": "AUD",
"Date": "2020-06-22",
"startCash": "0.000",
"Dep": "ABC90",
"sumCash": "770.87",
"AnotherDate": "2020-06-21"}
I am trying with regular expressions but its breaking the 'Date' fields.
jsonString.replaceAll(":[ ]*([\\w#\\.]+)", ": \"$1\"")
also tried with gson library, but its only putting the quotes on date values and not on the decimal values.
new JsonParser().parse(jsonString).toString()
What exactly I need to do to achieve it?
check this out
var newS = jsonString.replaceAll(": +((?!\\\\).*)(!?[,|}] *)", ": \"$1\"$2").replaceAll("\\\"\\\"","\"");
output
{
"Id": "2017",
"Currency": "AUD",
"Date": "2020-06-22",
"InCash": "0.000",
"Dep": "ABC90",
"sumCash": "770.87",
"AnotherDate": "2020-06-21"
}
if the other JSON form (rules) of yuior string don't change, this will work for the
name - value form.
note - i did not do the part where it could be a string in a list but its doable alsero
Find:
("\s*\w+\s*"\s*:(?!\s*")(?!\s*\d+\s*(?:,|\]|}))(?!\s*[{\[])(?!\s*(?:true|false|null)\s*(?:,|\]|}))\s*)(.+?)(?=[,}])
Replace:
$1"$2"
demo
see my complete PCRE regex for parsing JSON a eally simple structure spec.
here https://regex101.com/r/H8datD/1 which is not available to java until regex engine uses recursion (functions).
note - when needing to change just one aspect of JSON where it otherwise is complient with the spec structure
it is easy to extract the code in my regex to get right down to the area of interest. it easy !
Got it working with the below regex expression.
jsonString.replaceAll(": [ ]*([\\w#\\.-]+)", ": \"$1\""));
Thanks everyone for your help and support !

Data ref treated as number in everit-org/json-schema library to validate JSONSchema

I am using https://github.com/everit-org/json-schema library to do serverside validation for JSONSchema it is failing when i use dataref in maximum validation
my schema property is like
"fromYear": {
"array": false,
"type": "number",
"title": "From Year",
"minimum": {
"$data": "1/toYear"
},
"key": "fromYear",
"required": false
}
but when we call validate method of Schema then it throws error like
"#/properties/qualificationsection/items/properties/fromYear/minimum: expected type: Number, found: JsonObject"
JSON Schema supports minimum number as well as a jsonobject (ie., it supports union types) where we can specify data ref.
The library is unable to deal with this as is expecting minumum to always be of type number
$data is not a JSON Schema keyword at current (draft-7).
Some libraries attempt to implement it, as it is a suggestion for future drafts, but it is not currently part of the official specification.
There's no alternative to this. It simply isn't currently possible.

How to/best practice on how to differentiate between serialised objects stored within JSON format?

Suppose that I serialise two different objects and save them to a directory.
Problem: Upon application start up, parsing the JSON files are not a problem - since GSON is employed, I can write my own serialisers and deserialisers for both of the JSON files for their respective objects to be constructed.
But the problem is, how can I differentiate between the numerous JSON files in terms of what they store within them, so I can apply the correct deserialiser to it.
Thank you, best.
Consider standardizing your JSON structure to include document type. You can even store the target object type in that field. Good practice is to include document version number as well. Example below shows two different versions of the 'account' document and a transaction document. All three can be stored in, say, the same Couchbase bucket. The way to differentiate between different documents would be to look at the "doc_type" field and the document version (if required). From the GSON serializer selection standpoint, you can look at at the "doc_type" in a switch/if-else statement or store the target object type in place of "account" or "transaction" and then, at the expense of performance, dynamically parse JSON to POJO.
{
"doc_type": "account",
"doc_ver": 1,
"content": {
"accnt_no": "12321645645484",
"name": "Name or alias",
"email": "Email address",
"password": "Password in raw format",
"exp_date": "06/10/2017"
}
}
{
"doc_type": "account",
"doc_ver": 2,
"content": {
"accnt_no": "12321645645484",
"name": "customer name",
"email": "customer email",
"password": "pass",
"timezone": "customer timezone",
"ip": "IP address",
"spoken_languages": [ "EN", "RU" ],
"exp_date": "06/10/2017"
}
}
{
"doc_type": "transaction",
"doc_ver": 1,
"content": {
"accnt_no": "12321645645484",
"tran_date": "06/04/2017",
"tran_time": "09:15:84.953"
}
}
Hope this helps.
I think that the best way is parse JSON to a HashMap<String, Object> with multiple level. GSON will parse your JSON to HashMap with key is object name and value is an object (This object will belong to 3 type: HashMap for a object in JSON, List for an array in JSON and String for a string in JSON). To using this HashMap you need to iterate through the HashMap using a recursive method.

Mapping JSON Array to Java POJO

How would i map this JSON to an object?
{"d":"[{\"Key\":\"3\",\"ExternalKey\":\"143234A\",\"Name\":\"cup of juice\",\"Litres\":\"2 litres\",\"Date\":\"2016-10-06T08:32:27\",\"Capacity\":5.4900,\"CapacityType\":\"%\"}, {\"Key\":\"3\",\"ExternalKey\":\"143234A\",\"Name\":\"cup of milk\",\"Litres\":\"2.4 litres\",\"Date\":\"2016-10-06T08:32:27\",\"Capacity\":1667.6100,\"CapacityType\":\"M\"}]"}
I've tried using a HashMap but it just puts "d" as the string and the rest as a String object with one element
This is an extremely common problem known as data marshaling. In java, Jackson is the general best solution. Read this tutorial: http://wiki.fasterxml.com/JacksonInFiveMinutes
Your Json string has some extra " characters.
This is the final json:
{
"d": [
{
"Key": "3",
"ExternalKey": "143234A",
"Name": "cup of juice",
"Litres": "2 litres",
"Date": "2016-10-06T08:32:27",
"Capacity": 5.49,
"CapacityType": "%"
},
{
"Key": "3",
"ExternalKey": "143234A",
"Name": "cup of milk",
"Litres": "2.4 litres",
"Date": "2016-10-06T08:32:27",
"Capacity": 1667.61,
"CapacityType": "M"
}
]
}
Now you can copy the json and paste it here to get the pojo.
There are n numbers of libraries available for parsing the JSON and convert into in java classes.
Some of the examples are,
GSON
Jackson
Logan Square
Moshi, etc
You need to create a java class which map with your JSON Response and need to change your pojo according to parsing library.
For Example,
Using Logan Square you must annotate your POJO class to #JsonObject and all properties must be annotated with #JsonField.
Now, you can parse your json using method
...
LoganSquare.parse(jsonString, pojoClassName);
...
Hopefully this will help you.
Cheers
if i substring out the "d" key and map it as a JSON Array it works, that'll have to be the solution for now...

JSON Object can't be converted to JSON Array

ya i know that it's very usual problem while mapping but my problem is some different hear is the scenario
when my response have the data it gives me JSON Response like this
{
"responseID": "110",
"resultSet": [
{
"USERNAME": "Aninja",
"position": "Developer",
"salary": "60000"
}
],
"isSuccessful": true,
"rtnCode": "0000"
}
and below is the same JSON response when data is not found
{
"responseID": "123",
"resultSet": {},
"isSuccessful": true,
"rtnCode": " "
}
as i can see hear when response have some data result set have JSON Array but when no data found we have JSON Object as a response
so this is the reason I'm getting this problem.
so my question is that how should i handle this problem thanks for your response
Edit: the main problem is that i have made my model like list of JSON Object it works fine when there is result but it gives me error Can't convert JSON Object to JSON Array when result is empty s please suggest me how can i hanle it I'm using Jackson 2.2 i have also tried #JsonInclude(Include.NON_EMPTY) and #JsonInclude(Include.NON_NULL)
I wouldn't say it is mistake from server or back-end. But it is always a good practice to provide appropriate "Null Object Pattern" which describes the uses of such objects and their behavior.
So for better practice array which doesn't have any values should be sent back using "[]". So in this case "resultSet" should be given as [] instead of {} so it can be easily understood at front-end.
There are number of examples here which shows why it is useful to follow Null Object Pattern.
For example, if you are returning count in you response and there is no count then it is better to use "0" instead of "null".

Categories