Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    anotherPassword
    @anotherPassword
    Fixed it with "{{ msg | to_json | from_json | json_query(query) }}", is the correct way to address this or is thera fix?
    Ryan Merolle
    @ryanmerolle
    hello all. I have a question about grouping keys in a nested dictionary
    [ 
       { 
          "name":1,
          "endpoints":[ 
             { 
                "switch":"switch-1",
                "ip":"192.168.1.1/30"
             },
             { 
                "switch":"switch-2",
                "ip":"192.168.1.2/30"
             }
          ]
       },
       { 
          "name":2,
          "endpoints":[ 
             { 
                "switch":"switch-1",
                "ip":"10.0.0.1/30"
             },
             { 
                "switch":"switch-3",
                "ip":"10.0.0.2/30"
             }
          ]
       }
    ]
    I want to loop to create a list similar to [["192.168.1.1/30" , "192.168.1.2/30" ],["10.0.0.1/30" , "10.0.0.2/30" ]]
    I am able to get list of all ips under endpoints as 1 list using [].endpoints[].ip
    but I want in this case, 2 lists (or however many dictionaries that I find in the above list)
    thanks as always
    nevermind!
    [].endpoints.[*].ip does the trick...answered my own question
    Doqnach
    @Doqnach
    @ryanmerolle [].endpoints[*].ip gets you in the right direction
    ow you fixed it already :-P
    Ryan Merolle
    @ryanmerolle
    thanks @Doqnach
    Doqnach
    @Doqnach
    think you have a . too much
    Ryan Merolle
    @ryanmerolle
    yea that was a typo on chat
    Mitch Schooler
    @MitchSchooler_twitter
    Hi, I'm trying to use jmespath within a Tavern API test executed by pytest and I don't seem to be able to use the search() function. Other jmespath functions are working fine but for some reason I keep getting "jmespath.exceptions.UnknownFunctionError: Unknown function: search()" when trying to use the search function. Any idea why this one would not be found when other jmespath functionality is working as expected?
    I'm trying to do something like:
              function: tavern.testutils.helpers:validate_content
              extra_kwargs:
                comparisons:
                  - jmespath: "search(members, @)"
    Chris Armstrong
    @chris-armstrong
    jmespath itself doesn't have a search() function - this is typically the API name for calling JMESPath with an object and expression.
    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'] }