简体   繁体   English

如何根据属性值过滤json的元素

[英]How to filter elements of json based on attribute values

I have a json schema with many elements with attribute "image". 我有一个json模式,其中有许多元素具有属性“ image”。

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

and actual json looks like below 和实际的json看起来像下面

  "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 是否有可能基于属性“ image”过滤架构并以jsonpath格式获取所有元素的列表

$.a.b.c.passportPhoto

I have to read json using json path and then do something about photo like serialize it. 我必须使用json路径读取json,然后对照片进行一些处理,例如将其序列化。 but my question is about how to filter schema based on "image" attribute in Java system. 但是我的问题是关于如何在Java系统中基于“图像”属性过滤模式

To do that you can use Jayway JsonPath library. 为此,您可以使用Jayway JsonPath库。 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. 描述JSON JSON Schema也是有效的JSON因此,首先,您可以从JSON Schema提取所有属性,然后再处理给定的JSON有效负载。 In below example I use predefined list of properties. 在下面的示例中,我使用预定义的属性列表。

For given JSON payload (assume that all *photo properties are described in schema as images): 对于给定的JSON有效负载(假设所有*photo属性在模式中均以图片描述):

{
  "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 Jsonpath与Jackson或Gson
  2. Json Path Maven Json Path Maven

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM