gjson icon indicating copy to clipboard operation
gjson copied to clipboard

Filtering by child arrays

Open adaml881 opened this issue 4 years ago • 4 comments

Using below as an example

[
    {
        "Name": "John Smith",
        "Attributes": [
            {
                "Type": "Age",
                "Value": 30
            },
            {
                "Type": "Height",
                "Value": 170
            }
        ],
    },
    {
        "Name": "Anne Smith",
        "Attributes": [
            {
                "Type": "Age",
                "Value": 33
            },
            {
                "Type": "Height",
                "Value": 167
            }
        ],
    },
]

I'm looking for a way to filter an array by child objects. For example, I want to find the person that is aged 30.

I want to be able to filter on Attributes.Type == "Age" and Attributes.Value == 30, and I need it to return the full object

    {
        "Name": "John Smith",
        "Attributes": [
            {
                "Type": "Age",
                "Value": 30
            },
            {
                "Type": "Height",
                "Value": 170
            }
        ],
    }

Hope that makes sense. Thanks

adaml881 avatar Apr 06 '21 13:04 adaml881

I think this is what you are looking for.

#(Attributes.#(Type=="Age"))#|#(Attributes.#(Value==30))#

The #(Attributes.#(Type=="Age"))# returns the objects that have an Attributes member with Type == "Age" The | allows for doing another filter, which in this cast works like and operation. The #(Attributes.#(Value==30))# returns the full objects that have an Attributes member with Value == 30

tidwall avatar Apr 08 '21 01:04 tidwall

Yo can I get a certain amout of outputs starting from a certain cursor using gjson ?

ghost avatar Apr 17 '21 11:04 ghost

Hi @amirhaouala

I would like to help but I'll need more information about what you are trying to achieve.

Is your question related to this issue? If not, it would be helpful to open a new issue and describe your question in detail.

tidwall avatar Jun 07 '21 16:06 tidwall

Using below as an example

[
    {
        "Name": "John Smith",
        "Attributes": [
            {
                "Type": "Age",
                "Value": 30
            },
            {
                "Type": "Height",
                "Value": 170
            }
        ],
    },
    {
        "Name": "Anne Smith",
        "Attributes": [
            {
                "Type": "Age",
                "Value": 33
            },
            {
                "Type": "Height",
                "Value": 167
            }
        ],
    },
]

I'm looking for a way to filter an array by child objects. For example, I want to find the person that is aged 30.

I want to be able to filter on Attributes.Type == "Age" and Attributes.Value == 30, and I need it to return the full object

    {
        "Name": "John Smith",
        "Attributes": [
            {
                "Type": "Age",
                "Value": 30
            },
            {
                "Type": "Height",
                "Value": 170
            }
        ],
    }

Hope that makes sense. Thanks

Is it possible to filter above object by Type where Type can be "Age" or "Height"

sravyaparsi avatar Jul 11 '21 15:07 sravyaparsi