tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.1k stars 846 forks source link

can it support that finding all matches with wildcard? #229

Open Elvins opened 3 years ago

Elvins commented 3 years ago

can it support that finding all matches with wildcard? I found that wildcards only returns the first result.

brincowale commented 3 years ago

It seems that isn't possible based on this comment

As I had the same problem, I decided to use a for each in combination with a @this

My JSON

{
    "action": "Products",
    "product0001": {
        "id": "123456",
        "name": "alex"
    },
    "product00012": {
        "id": "123456",
        "name": "alex"
    },
    "product0003": {
        "id": "123456",
        "name": "alex"
    }
}

My solution

result := gjson.Get(string(resp.Body(), "@this")
for _, product := range result.Map() {
    id := gjson.Get(product.String(), "id")
    name := gjson.Get(product.String(), "name")
}