Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Chris Armstrong
    @chris-armstrong
    You probably just want a filter expression i.e. array[?some_property == 'value1']
    Mitch Schooler
    @MitchSchooler_twitter
    Okay, thanks for that clarification. I'm sure I'm just missing something super obvious here, but I thought search() was how you accessed the value of a particular key in a map. What is the proper way to do this? For example, how do I get "bar" out of this map the way that this search() example shows without search()?
    search(foo, {"foo": "bar"}) -> "bar"
    Chris Armstrong
    @chris-armstrong
    bar - literally just the property name
    Mitch Schooler
    @MitchSchooler_twitter
    In my case I only know the expected key (foo) and its value could vary. How do I get the value out of the map using the foo key?
    Chris Armstrong
    @chris-armstrong
    sorry I meant foo
    Mitch Schooler
    @MitchSchooler_twitter
    Oh, so like: @[foo] ?
    Chris Armstrong
    @chris-armstrong
    no just foo or @.foo
    Mitch Schooler
    @MitchSchooler_twitter
    Gotcha, I'll give that a try. Thanks!
    Chris Armstrong
    @chris-armstrong
    @[foo] gives you ['bar']
    Mitch Schooler
    @MitchSchooler_twitter
    I see. So how would I get "ids" from this nested example? Would it just be @.members.ids and it should give me back ["id1", "id2"] ?
    {
        "members": {
            "ids": [
                "id1",
                "id2"
            ]
        }
    }
    Chris Armstrong
    @chris-armstrong
    yep
    sinusx
    @sinusx_gitlab
    Hi! Week ago I have found online tool where I can make query by jmespath language, now I can not find it again. Please provide link if you know?
    jdevillard
    @jdevillard
    Hello you’ve different tool available
    The site jmespath.org with de javascript impl
    sinusx
    @sinusx_gitlab
    @jdevillard Thank you!!!
    broskees
    @broskees
    Hey guys!
    Discovered JMESpath a few days ago
    It's helped me immensely already
    Quick question to whoever is available

    With a JSON object like the following

    [
        {
            "type": {
                text: "company"
            },
            "data": {
                "name": "Company A",
                "logo": "company_a.png",
                "divisions": "Division A, Division B, Division C",
                "homepage": "company-a.com"
            }
        },
        {
            "type": {
                text: "company"
            },
            "data": {
                "name": "Company B",
                "logo": "company_b.png",
                "divisions": "Division A, Division B, Division C",
                "homepage": "company-b.com"
            }
        },
        {
            "type": {
                text: "employee"
            },
            "data": {
                "name": "John Smith",
                "dob": "1/16/1990",
                "position": "CEO"
            }
        },
        {
            "type": {
                text: "company"
            },
            "data": {
                "name": "Company C",
                "logo": "company_c.png",
                "divisions": "Division A, Division B, Division C",
                "homepage": "company-c.com"
            }
        },
        {
            "type": {
                text: "employee"
            },
            "data": {
                "name": "Oprah",
                "dob": "1/16/1990",
                "position": "TV Personality"
            }
        }
    ]

    Is there a JMESpath query I can use to reformat the above string to display more like this?

    {
        companies: [
            {
                "type": {
                    text: "company"
                },
                "data": {
                    "name": "Company A",
                    "logo": "company_a.png",
                    "divisions": "Division A, Division B, Division C",
                    "homepage": "company-a.com"
                }
            },
            {
                "type": {
                    text: "company"
                },
                "data": {
                    "name": "Company B",
                    "logo": "company_b.png",
                    "divisions": "Division A, Division B, Division C",
                    "homepage": "company-b.com"
                }
            },
            {
                "type": {
                    text: "company"
                },
                "data": {
                    "name": "Company C",
                    "logo": "company_c.png",
                    "divisions": "Division A, Division B, Division C",
                    "homepage": "company-c.com"
                }
            }
        ],
        employees: [
            {
                "type": {
                    text: "employee"
                },
                "data": {
                    "name": "John Smith",
                    "dob": "1/16/1990",
                    "position": "CEO"
                }
            },
            {
                "type": {
                    text: "employee"
                },
                "data": {
                    "name": "Oprah",
                    "dob": "1/16/1990",
                    "position": "TV Personality"
                }
            }
        ]
    }
    broskees
    @broskees

    Basically I want to filter the objects by the type.text, and display the top level object containing that type.

    I've been playing with the contains function, which does allow me to filter, however the return result is only the current node, I need to return the containing parent node.

    Is this possible with JMESpath?

    Chris Armstrong
    @chris-armstrong
    It seems it is: { companies: [?type.text=='company'], employees: [?type.text=='employee'] }
    Evgeny Zislis
    @kesor
    Any way to query a field that is sometimes a single and sometimes a list? Like sometimes it is { "item": { "a" : "b" } } but sometimes it is { "item": [ {"a":"b"}, {"c":"d"} ] } and I would like to always convert it to an array/list when it is in the first representation
    Ah, looks like I found a way ... first always convert to array, then flatten. Like this [SomePathWhereItemsAre.item][] takes care of a single item, and of multiple items - always returns a list (of one or more).
    broskees
    @broskees
    @chris-armstrong That worked!! How would you do it if the beginning string was an object, like so?
    {
        "0": {
            "type": {
                "text": "company"
            },
            "data": {
                "name": "Company A",
                "logo": "company_a.png",
                "divisions": "Division A, Division B, Division C",
                "homepage": "company-a.com"
            }
        },
        "1": {
            "type": {
                "text": "company"
            },
            "data": {
                "name": "Company B",
                "logo": "company_b.png",
                "divisions": "Division A, Division B, Division C",
                "homepage": "company-b.com"
            }
        },
        "2": {
            "type": {
                "text": "employee"
            },
            "data": {
                "name": "John Smith",
                "dob": "1/16/1990",
                "position": "CEO"
            }
        },
        "3": {
            "type": {
                "text": "company"
            },
            "data": {
                "name": "Company C",
                "logo": "company_c.png",
                "divisions": "Division A, Division B, Division C",
                "homepage": "company-c.com"
            }
        },
        "4": {
            "type": {
                "text": "employee"
            },
            "data": {
                "name": "Oprah",
                "dob": "1/16/1990",
                "position": "TV Personality"
            }
        }
    }
    Chris Armstrong
    @chris-armstrong
    I think you need an extra [] operator e.g. { companies: [][?type.text=='company'], employees: [][?type.text=='employee'] }
    broskees
    @broskees
    Hm, that doesn't seem to be working for me. Last question
    Just returns { companies: null, employees: null }
    Chris Armstrong
    @chris-armstrong
    oh I thought the flatten operator would just give the values.
    try values(@) | { companies: [?type.text=='company'], employees: [?type.text=='employee'] }
    kguggulotu
    @kguggulotu

    I'd like to use jmespath to transform json from:

    {
        "order": {
            "order_type": "online",
            "item": [
                {
                    "item_nbr": 1,
                    "item_type": "so",
                    "mlog": [
                        {
                            "id":"1",
                            "timestamp":"2019-12-13"
                        },
                        {
                            "id":"2",
                            "timestamp":"2019-12-13"
                        }
                    ]
                },
                {
                    "item_nbr": 2,
                    "item_type": "so",
                    "mlog": [
                        {
                            "id":"1",
                            "timestamp":"2019-12-13"
                        },
                        {
                            "id":"3",
                            "timestamp":"2019-12-13"
                        }
                    ]
                }
            ],
            "mlog": [
                {
                    "id":"5",
                    "timestamp":"2019-12-13"
                },
                {
                    "id":"6",
                    "timestamp":"2019-12-13"
                }
            ]
        }
    }

    to

    [
        {
            "order": {
                "order_type": "online",
                "item": [
                    {
                        "item_nbr": 1,
                        "item_type": "so",
                        "mlog": [
                            {
                                "id": "1",
                                "timestamp": "2019-12-13"
                            },
                            {
                                "id": "2",
                                "timestamp": "2019-12-13"
                            }
                        ]
                    }
                ],
                "mlog": [
                    {
                        "id": "5",
                        "timestamp": "2019-12-13"
                    },
                    {
                        "id": "6",
                        "timestamp": "2019-12-13"
                    }
                ]
            }
        },
        {
            "order": {
                "order_type": "online",
                "item": [
                    {
                        "item_nbr": 2,
                        "item_type": "so",
                        "mlog": [
                            {
                                "id": "1",
                                "timestamp": "2019-12-13"
                            },
                            {
                                "id": "3",
                                "timestamp": "2019-12-13"
                            }
                        ]
                    }
                ],
                "mlog": [
                    {
                        "id": "5",
                        "timestamp": "2019-12-13"
                    },
                    {
                        "id": "6",
                        "timestamp": "2019-12-13"
                    }
                ]
            }
        },
        {
            "order": {
                "order_type": "online",
                "mlog": [
                    {
                        "id": "5",
                        "timestamp": "2019-12-13"
                    },
                    {
                        "id": "6",
                        "timestamp": "2019-12-13"
                    }
                ]
            }
        }
    ]
    Can someone help me with the query ?
    Chris Armstrong
    @chris-armstrong
    It looks like a fairly complicated transformation - you might want to break it down
    Rajat Taneja
    @rajataneja101
    Hi all, lets say i have a json like following: [
    {
    "Name": "JsonOneName",
    "Description": "JSONONEDESCRIPTION"
    },{
    "Name": "JsonTwoName",
    "Description": "JsonOneDescription"
    }
    ]
    Now the problem which i am facing is, in the above json both the blocks have a same json block with same description just the casing is different so how can i run my json query without the case sensitivity even though the description is same??
    alexanderinsa
    @alexanderinsa
    I'm new to working with jmespath and I was wondering if there was a way to select multiple dictionary keys based on if that key contains a string? So like {a:blah, aa:blah, c:notblah} I would like to flatten the results of the keys that contain an a?
    Chris Armstrong
    @chris-armstrong
    @alexanderinsa from what I can tell, this isn't something JMESpath supports
    alexanderinsa
    @alexanderinsa
    I couldn't find a way either, but my use case was with ansible , so I was able to use a filter to convert each key value pair to a list of dictionaries with a common key and was able to loop through and check the now common key.
    Ciaran Evans
    @ciaranevans

    Hi folks, I'm doing some boto3 work in Python and I have the following lines:

    paginator = client.get_paginator('list_objects_v2')
    page_iterator = paginator.paginate(Bucket=bucket_name)  # PaginationConfig MaxItems & PageSize left off intentionally
    filtered_iterator = page_iterator.search("Contents[?contains(Key, `{0}`)][]".format(mgrs_square))

    I get the following warning:

    jmespath/lexer.py:170: PendingDeprecationWarning: deprecated string literal syntax PendingDeprecationWarning)

    Is there a way I can change my JMESPath to not give that warning? - Completely new to this as it's not a functionality I've used before.

    Thanks!

    Chris Armstrong
    @chris-armstrong
    It might be worth raising a ticket with the maintainer on the project page @ciaranevans https://github.com/jmespath/jmespath.py
    Raghav Bijjula ☁️
    @BijjulaRaghav_twitter

    Hello,
    I want to yield priority value if a substring in TargetGroupArn matches from following JSON

    [
    {
    "Priority": "4",
    "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:app1/957d07a4bb7b4c3b"
    },
    {
    "Priority": "5",
    "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:app2/11f33cd58c040b74"
    },
    {
    "Priority": "default",
    "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:app3/b0a01b36e3cb45be"
    }
    ]

    Is there a way I can accomplish with jmespath ?
    Chris Armstrong
    @chris-armstrong
    [?TargetGroupArn=='arn:aws:elasticloadbalancing:us-east-1:app1/957d07a4bb7b4c3b'][0].Priority
    Erik Berg
    @eb4x_gitlab

    I'm leaving this here as a puzzle someone might help me solve. I have a netplan (converted from yaml), and I haven't quite figured out how to combine searches in these types of nested dicts.

    {
      "ethernets": {
        "enp3s0f0": {
          "match": {
            "macaddress": "01:23:45:67:89:ab"
          },
          "set-name": "eth0"
        },
        "enp3s0f1": {
          "match": {
            "macaddress": "cd:ef:01:23:45:67"
          },
          "set-name": "eth1"
        },
        "enp4s0f0": {
          "match": {
            "macaddress": "89:ab:cd:ef:01:23"
          },
          "set-name": "eth2"
        },
        "enp4s0f1": {
          "match": {
            "macaddress": "45:67:89:ab:cd:ef"
          },
          "set-name": "eth3",
          "addresses": [
            "172.16.0.3/24"
          ]
        }
      },
      "bonds": {
        "bond0": {
          "interfaces": [
            "eth0",
            "eth1"
          ],
          "parameters": {
            "mode": "802.3ad"
          }
        }
      },
      "vlans": [
        "bond0.1202",
        "bond0.203",
        "bond0.204",
        "bond0.205"
      ],
      "bridges": {
        "br-maas": {
          "addresses": [
            "172.16.1.3/24"
          ],
          "interfaces": [
            "bond0"
          ]
        },
        "br-mgmt": {
          "addresses": [
            "172.16.2.3/24"
          ],
          "interfaces": [
            "bond0.1202"
          ]
        },
        "br-storage": {
          "addresses": [
            "172.16.3.3/24"
          ],
          "interfaces": [
            "bond0.203"
          ]
        },
        "br-vxlan": {
          "addresses": [
            "172.16.4.3/24"
          ],
          "interfaces": [
            "bond0.204"
          ]
        },
        "br-iscsi": {
          "addresses": [
            "172.16.5.3/24"
          ],
          "interfaces": [
            "bond0.205"
          ]
        },
        "br-vlan": {
          "interfaces": [
            "eth2"
          ]
        }
      }
    }

    *.*.addresses will yield addresses, I can flatten it with [] but I suppose a [?addresses] type filter would be a better approach, so we only look for those with addresses. [keys(@)] gives me [ethernets, bonds, vlans, bridges], but *.[keys(@)] doesn't seem to give me the interface-names. But then *.*[keys(@)] gives me keys inside the interfaces.

    I'd like to get something that returns interface-name + address in some kind of combination. Like [ ["enp4s0f1", "172.16.0.3/24"], ["br-maas", "172.16.1.3/24"], ...]or [ {"enp4s0f1": ["172.16.0.3/24"]},{"br-maas":["172.16.1.3/24"]}, ...]

    Erik Berg
    @eb4x_gitlab
    So apparently the "vlans": [...] is causing problems? I removed it, and now I get something from *.[keys(@)]. I can probably work with this... Just fix the vlans in my lazy netplan.
    Gabriel
    @GaboFDC

    hey guys, not sure if this is the proper place, but just a suggestion in the contains examples would also be good to explain this syntax

    machines[?contains(guest_fullname, `Windows`) == `false`]

    for dicts like:

    {
      "machines": [
        {
          "guest_fullname": "Other (32-bit)",
          "age": 30
        },
        {
          "guest_fullname": "Microsoft Windows 10 (64-bit)",
          "age": 50
        },
        {
          "name": "Ubuntu",
          "age": 40
        }
      ]
    }

    I needed to do it, but it was really hard to come up with the proper syntax, wasn't even sure if it was possible.

    Dominic D'Apice
    @dapiced
    anyone can help me with the function max() ?
    Harbie34
    @Harbie34

    Hi, there!
    I'm trying to convert the result of AWS CloudWatch Logs Insights, but I can't.

    Like this:

    {
        "results": [
            [
                {
                    "field": "@timestamp",
                    "value": "2020-02-03 15:03:26.315"
                },
                {
                    "field": "@message",
                    "value": "some messages 1"
                },
                {
                    "field": "@ptr",
                    "value": "xxx"
                }
            ],
            [
                {
                    "field": "@timestamp",
                    "value": "2020-02-03 15:03:27.495"
                },
                {
                    "field": "@message",
                    "value": "some messages 2"
                },
                {
                    "field": "@ptr",
                    "value": "xxx"
                }
            ]
        ],
        "statistics": {
            "recordsMatched": 887.0,
            "recordsScanned": 887.0,
            "bytesScanned": 101532.0
        },
        "status": "Complete"
    }

    To this:

    [
        [ "2020-02-03 15:03:26.315", "some messages 1" ],
        [ "2020-02-03 15:03:27.495", "some messages 2" ]
    ]

    Can anyone help me?

    Chris Armstrong
    @chris-armstrong
    JMESPath makes this trickier than it needs to be, but here it is @Harbie34:
    map(&[([?field=='@timestamp'])[0].value, ([?field=='@message'])[0].value ], results)