2

I'm searching for a library which enables me to filter out a json instance in accordance with a json schema. If the json instance contains elements not defined in the schema they should be filtered out. I have found this for JavaScript: https://www.npmjs.org/package/json-schema-filter, but have been unable to find something that does this in Java.

Does anyone have suggestion as to how this can be achieved in Java? Or where to find a library that does the job?

Regards Morten

An example

File schemaname.json:

{
    "type": "object",
    "properties": {
        "aid": {
            "type": "string"
        }
    }
}

final String json =

{
    "aid" : "123954",
    "newfield" : "itsValue"
}

What I'm asking is if the filterInstance(instance,schema) method shown below exists.

JsonNode schema = JsonLoader.fromResource("path/schemaname.json");
JsonNode instance = (new ObjectMapper()).readTree(json);
JsonNode fInstance = filterInstance(instance,schema);

fInstance =

{
    "aid" : "123954"
}
mortenl
  • 109
  • 2
  • 8
  • Do you mean, that you don't know what kind of JSON you're dealing with and want to filter out JSON's that satisfy some schema? – mavarazy Nov 18 '14 at 12:07
  • I don't know the precise format of the incoming json and want my application to be able to exclude the attributes that it doesn’t care about. Just as shown in the example above. The `json` var includes a field, `newfield`, that is not a part of the contract in schemaname.json and therefore it is not included in the result, `fInstance`. – mortenl Nov 19 '14 at 09:37
  • did you get any answer for that? I'm too facing the same issue – Chathura Buddhika Jul 20 '20 at 04:48

3 Answers3

1

You can use https://github.com/chathurabuddi/json-schema-filter.
This is the Java version of the above-mentioned json-schema-filter

Chathura Buddhika
  • 2,067
  • 1
  • 21
  • 35
0

You can just ignore new fields in JSON, when you are mapping to POJO.

Ignoring new fields on JSON objects using Jackson

In this case your POJO is your Jackson scheme, and use Jackson for serialization in this case :)

Community
  • 1
  • 1
mavarazy
  • 7,562
  • 1
  • 34
  • 60
  • I see your point but I'm having trouble converting your idea in my concrete example. I have updated the question and hope that you will be so kind as to elaborate a bit for me in the context of my example? – mortenl Nov 18 '14 at 12:05
0

This questions was posted long ago and specification may have changed but as of JSON Schema 2020-12 the JSON in the example above is valid and no fields should be removed according to the schema.

The JSON schema of schemaname.json in the example above implies that the JSON String is valid because keyword "additionalProperties" has default value of empty schema {}.

I have not found any JSON Schema filter that filters based on the full schema so I am about to create my own implementation for this but would be happy if anyone knows any JSON Schema filter implementation written in Java (or other JVM-based language).