flomesh-io / pipy

Pipy is a programmable proxy for the cloud, edge and IoT.
https://flomesh.io/pipy
Other
743 stars 70 forks source link

support jsonpath tools for get value from json #156

Closed sixinyiyu closed 1 year ago

sixinyiyu commented 1 year ago

Discussion

for example:

response:
{
    "store":{
        "book":[
            {
                "category":"reference",
                "author":"Nigel Rees",
                "title":"Sayings of the Century",
                "price":8.95
            },
            {
                "category":"fiction",
                "author":"Evelyn Waugh",
                "title":"Sword of Honour",
                "price":12.99
            },
            {
                "category":"fiction",
                "author":"Herman Melville",
                "title":"Moby Dick",
                "isbn":"0-553-21311-3",
                "price":8.99
            },
            {
                "category":"fiction",
                "author":"J. R. R. Tolkien",
                "title":"The Lord of the Rings",
                "isbn":"0-395-19395-8",
                "price":22.99
            }
        ],
        "bicycle":{
            "color":"red",
            "price":19.95
        },
        "clothes":[
            {
                "name":"牛仔裤",
                "sizes":"S",
                "price":94
            },
            {
                "name":"背心",
                "sizes":"M",
                "price":48
            },
            {
                "name":"裙子",
                "sizes":["S", "M"],
                "price":1.24
            },
            {
                "name":"羊毛衫",
                "sizes":["XS", "XL"],
                "price":78.99
            },
            {
                "name":"Polo衫",
                "sizes":["XS", "XL", "M"],
                "price":18.99
            }
        ]
    },
    "expensive":10
}

 console.log(JsonPath.read(response, "$.store.book[*].author"))

output the all author
pajama-coder commented 1 year ago

Hi @sixinyiyu,

Implementing such an API involves a lot of work. We must take into consideration how many widely demanded use-cases this can support and how much work it requires and compare those to make a decision. For your example tho, it's pretty easy to work around by doing:

store.book.map(book => book.author)

which gives the same result.