简体   繁体   English

JSON路径使用JSON路径从父对象和子对象中提取特定字段/数组

[英]JSON Path extract specific fields/array from parent and child object using JSON Path

Given a JSON object like: 给定一个JSON对象,例如:

"book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "book_id": "214515",
                "title": "Sayings of the Century",
                "price": 8.95,
                "reviews": [
                {
                  "rating": 2,
                  "reviewer": "Amazon",
                  "weight": 0
                },
                {
                ...
                }
            ]
        },
        {
        ...
        }

Is it at all possible to select book_id, along with part or all of the reviews for that book? 是否可以选择book_id以及该书的部分或全部评论?

The result might look something like this: 结果可能如下所示:

[
    {
    "book_id": "...",
    "reviews": [
        ...
    ]
    },
    {
        ...
    }
]

I've been using Jayway jsonpath: https://github.com/json-path/JsonPath 我一直在使用Jayway jsonpath: https : //github.com/json-path/JsonPath

The following is not suitable a solution when dealing with arrays, like the 'reviews', and then joining programmatically: 在处理数组(例如“评论”)然后以编程方式加入时,以下不适合作为解决方案:

   List ids = JsonPath.read(json, "$.store.books[*].book_id");
   List reviews =  JsonPath.read(json, "$.store.books[*].reviews[*]");

Following the doc at https://github.com/json-path/JsonPath , this should give you what you are looking for. 遵循https://github.com/json-path/JsonPath的文档,这应该可以为您提供所需的内容。

List output = JsonPath.read(json, "$.books[*].['book_id', 'reviews'])");

Have a look at this test case. 看一下这个测试用例。

@Test
final void test2() {
    String json = "{ \"books\": [" 
            + "                    {" 
            + "                        \"category\": \"reference\","
            + "                        \"author\": \"Nigel Rees\","
            + "                        \"book_id\": \"214515\","
            + "                        \"title\": \"Sayings of the Century\","
            + "                        \"price\": 8.95," 
            + "                        \"reviews\": ["
            + "                        {" 
            + "                          \"rating\": 2,"
            + "                          \"reviewer\": \"Amazon\"," 
            + "                          \"weight\": 0"
            + "                        }" 
            + "                      ]" 
            + "                    },"
            + "                    {" 
            + "                        \"category\": \"reference\","
            + "                        \"author\": \"Nigel Rees\","
            + "                        \"book_id\": \"314515\","
            + "                        \"title\": \"Sayings of the Century\","
            + "                        \"price\": 8.95," 
            + "                        \"reviews\": ["
            + "                        {" 
            + "                          \"rating\": 4,"
            + "                          \"reviewer\": \"Trip\"," 
            + "                          \"weight\": 5"
            + "                        }" 
            + "                      ]" 
            + "                    }"

            + "               ]" 
            + "}";

    List output = JsonPath.read(json, "$.books[*].['book_id', 'reviews'])");

    String expectedOutput = 
            "["
            + "{"
                + "\"book_id\":\"214515\","
                + "\"reviews\":["
                                + "{"
                                        + "\"rating\":2,\"reviewer\":\"Amazon\",\"weight\":0"
                                + "}"
                            + "]"
            + "},"
            + "{"
                + "\"book_id\":\"314515\","
                + "\"reviews\":["
                                + "{\"rating\":4,\"reviewer\":\"Trip\",\"weight\":5}"
                            + "]"
            + "}"
        + "]";

    assertEquals(expectedOutput, output.toString());

}   

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

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