0

I have a json schema with many elements with attribute "image".

  "passportPhoto": {
    "description": "Passport photo",
    "type": "string",
    "image": {
      "binaryEncoding": "base64"
    }
  },

and actual json looks like below

  "passportPhoto": "photo in base 64 encoded string format",

Is it possible to filgter schema based on attribute "image" and get list of all the elements in jsonpath format

$.a.b.c.passportPhoto

I have to read json using json path and then do something about photo like serialize it. but my question is about how to filter schema based on "image" attribute in Java system.

Sammy Pawar
  • 1,201
  • 3
  • 19
  • 38
  • I am not sure it is possible straightaway. You need to read schema and find all properties which has `image` attribute. After that you need to find all nodes for given list of attributes. If schema is constant you can manually extract these properties and just try to find all nodes for predefined list of properties. – Michał Ziober Feb 14 '19 at 11:03
  • 1
    I don't have constant schema. There are many applications and all have different schema. But they agreed that they will send all images tagged as "image" in schema. I am sure many would have faced similar problem. – Sammy Pawar Feb 14 '19 at 11:18
  • See my below example. It works for predefined set of attributes, but using this example you should be able to extend it to read schema first and after that use properties you found int schema to find paths and values in `JSON` payload. – Michał Ziober Feb 14 '19 at 11:41
  • @MichałZiober Thanks a ton. It is really helpful. I am getting Path: $['chip']['properties']['passportPhoto']['image'] But I want $['chip']['passportPhoto'] . – Sammy Pawar Feb 14 '19 at 15:10

1 Answers1

1

To do that you can use Jayway JsonPath library. It allows to find paths for given property. When you find all paths you can extract values for them. JSON Schema which describes JSON is also a valid JSON so firstly, you can extract all properties from JSON Schema and after that proces given JSON payload. In below example I use predefined list of properties.

For given JSON payload (assume that all *photo properties are described in schema as images):

{
  "map": {
    "photo": "map photo"
  },
  "person": {
    "data": {
      "photos": {
        "photo": "photo Base64",
        "passportPhoto": "passport photo Base64"
      }
    }
  }
}

Below example:

import com.jayway.jsonpath.EvaluationListener;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JsonPathApp {
    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        List<String> paths = new ArrayList<>();
        ReadContext findPathsContext = JsonPath.parse(jsonFile).withListeners((found) -> {
            paths.add(found.path());
            return EvaluationListener.EvaluationContinuation.CONTINUE;
        });

        List<String> properties = Arrays.asList("photo", "passportPhoto");
        properties.forEach(p -> findPathsContext.read("$.." + p));

        ReadContext readContext = JsonPath.parse(jsonFile);

        for (String path : paths) {
            System.out.println("Path: " + path);
            System.out.println("Value: " + readContext.read(path));
        }
    }
}

Prints:

Path: $['map']['photo']
Value: map photo
Path: $['person']['data']['photos']['photo']
Value: photo Base64
Path: $['person']['data']['photos']['passportPhoto']
Value: passport photo Base64

See also:

  1. Jsonpath with Jackson or Gson
  2. Json Path Maven
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • Thank you so much. This is almost what I need. could you please further help me My schema is something like this "chip": { "type": "object", "properties": { "chip ": { "passportPhoto": { "description": "Passport photo", "type": "string", "image": { "binaryEncoding": "base64" } }, I am getting Path: $['chip']['properties']['passportPhoto']['image'] But I want $['chip']['passportPhoto'] – Sammy Pawar Feb 14 '19 at 14:41
  • @SammyPawar, Replace `['properties']` and `['image']` with `EMPTY` `String` and you will get what you want. – Michał Ziober Feb 14 '19 at 15:12