ネストされた配列のexistance及びその中にネストされたアイテムの値をベースフィルタJSONに使用JQ

デレク・ビーティー:

以下のJSONを考えると、

私は、アイテムのコメント配列がnullの車の列でアイテムをフィルタリングしようとしています:

.cars[] |  select(.comments == null)

またはアイテムのコメント配列が存在するとコメントオブジェクトのいずれかに値が含まれていない「FooBarの」

.cars[] |  select( select(.comments != null) | (any(.comments[]; index("FooBar")) | not) )

元の構造を保持したまま。

JQで、私は私がに絞り込むしたいが、私は周りに私の頭をラップすることはできませんが、トップレベルのアイテムに適用されるようにする方法であるcriteraを作成する方法を見つけ出すことができます。

JSON入力:

{
    "person": {
        "first_name": "Bob",
        "last_name": "Smith"
    },
    "addresses": [
        {
            "home": {
                "line1": "123",
                "line2": "A st."
            }
        },
        {
            "work": {
                "line1": "456",
                "line2": "B st."
            }
        }
    ],
    "cars": [
        {
            "make": "Honda",
            "model": "Civic"
        },
        {
            "make": "Honda",
            "model": "Accord"
        },
        {
            "make": "Honda",
            "model": "Pilot",
            "comments": [
                "Comment 1",
                "Comment 2",
                "FooBar"
            ]
        },
        {
            "make": "Honda",
            "model": "Passport",
            "comments": [
                "Comment 3",
                "Comment 4"
            ]
        }
    ]
}

コメントアレイは「FooBarの」値をcontaingと入力しかしoject同じJSON Oututは、除外されます。

{
    "person": {
        "first_name": "Bob",
        "last_name": "Smith"
    },
    "addresses": [
        {
            "home": {
                "line1": "123",
                "line2": "A st."
            }
        },
        {
            "work": {
                "line1": "456",
                "line2": "B st."
            }
        }
    ],
    "cars": [
        {
            "make": "Honda",
            "model": "Civic"
        },
        {
            "make": "Honda",
            "model": "Accord"
        },
        {
            "make": "Honda",
            "model": "Passport",
            "comments": [
                "Comment 3",
                "Comment 4"
            ]
        }
    ]
}
イスマイルOGUZ:

ただ、使用更新代入演算子を(|=)。

.cars |= map(select(.comments))
.cars |= map(select(.comments and any(.comments[]; index("FooBar")) | not))

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=33736&siteId=1