Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Yoandy Rodriguez Martinez
    @yorodm
    I have a JSONLines file I would like to parse using jp
    Chris Armstrong
    @chris-armstrong
    I think the jmespath expression parser doesn't deal with \r characters, only \n characters as whitespace.
    strangeglyph
    @strangeglyph
    Hey, I have a list of objects of the form [{id: 0, name: foo}, {id: 1, name: bar}] and I'd like to rewrite that list to a dict like {foo: 0, bar: 1}. Can I do that with jmespath alone?
    I tried to do something like map(&{@.name: @.id}, mylist) but apparently I can't use @.name as key?
    John Arrowwood
    @JohnArrowwood
    Can JMESPath be used to just tweak a value? Using the sample from the home page, if I wanted to return the input, but with one or more values changed, how would I do that?
    So, let's move New York to HI, and Seattle to NM, but otherwise return the JSON as-is
    kmfpo
    @kmfpo

    Hello all. I am having trouble writing a test query in jmespath when my field has a . in it. Any idea how to get this to work? If I change vmfacts.to vmfacts_ it works fine...

    query: vmfacts.virtual_machines[*].{Name: guest_name, IP: ip_address}
    data:
    { "vmfacts.virtual_machines": [ { "attributes": {}, "cluster": "My Cluster", "esxi_hostname": "10.22.22.22", "guest_fullname": "Red Hat Enterprise Linux 7 (64-bit)", "guest_name": "d2l03fsx0", "ip_address": "1.2.3.4" } ] }

    Buddy Rikard
    @TpyoKnig

    @JohnArrowwood you could do something like this:

    {"City": "New York"}

    [(City != 'New York') && 'Hi'] | [0] would return HI rather than New York

    James Haggerty
    @wryun
    @kmfpo you need to quote the field in your query - "vmfacts.virtual_machines"
    jrvilda
    @jrvilda
    How are objects of an object listed?
    data-> { "hostvar": { "equipo1":{ "atributo1":"a"},"equipo2":{ "atributo1":"b"},"equipo3":{"atributo1":"a"}}}
    expected result-> ["equipo1", "equipo3"]
    fail Attempt -> hostvar[?atributo1 == 'a']
    jrvilda
    @jrvilda
    HI
    jrvilda
    @jrvilda
    hostvar.*.atributo1[?contains(@, 'a')] -> [ "a", "a" ]
    but how do I get the keys (@)
    jrvilda
    @jrvilda
    hostvar.*.atributo1[?contains(@, 'a')][key@] -> [ [ null, "a" ], [ null, "a" ] ]
    Rajanusa
    @Rajanusa

    Hi,

    My Data is
    {
    "title": "title1",
    "description": "description1",
    "blog": "This is a blog.",
    "date": "11/4/2013"
    }
    My Expression is
    {
    "data":{
    "date":date,
    "text":blog
    },
    "name":title,
    "description":description
    }

    It is not working since the expression is formatted and has \r\n.

    If I give expression as single line minified, It works.

    Can you advise how to work with formatted expression that has json path and static string to form a valid JSON.

    Rajanusa
    @Rajanusa
    The following is my node.js code
    app.post('/JMESPath', function (req, res, next) {
    const data = req.body.data;
    const map = req.body.map;
    var result = jmespath.search(data, map);
    res.send(result);
    });
    jrvilda
    @jrvilda
    I do not give with it. This is possible?
    show keys where atributo1 =='a'
    data-> { "hostvar": { "equipo1":{ "atributo1":"a"},"equipo2":{ "atributo1":"b"},"equipo3":{"atributo1":"a"}}}
    expected result-> ["equipo1", "equipo3"]
    cytort
    @cytort
    hi, i'm using the online tool and have already made my filter but when i tried it in javascript. it doesn't work anymore
    my filter [].modems[].us
    cytort
    @cytort
    my data is.. [ {"ds": [ { "freq":100,"mod":"none" } ] , "modems": [ { "us": [ { "snr":20,"ucw":0, } ] } ] }]
    bjosafa
    @bjosafa
    Hello
    Could someone help me with a doubt?
    I have this array: [{"List":[{"Type":0},{"Type":1},{"Type":2}],"Title":{"Value":"Title 1"}},{"List":[{"Type":0},{"Type":1},{"Type":2}],"Title":{"Value":"Title 2"}}]
    And I want only get results with the type is equal to "1"
    I have tried this filter [?List[?Type==1]] | [].List[].Type , but it is returning all types...
    Chris Armstrong
    @chris-armstrong
    @bjosafa What about [].List[?"Type"== `1`]
    Jugaadi
    @jugaadi
    Is it possible to set a default projection to be used for all arrays? For example, use Flatten projection by default. i.e reservations.instances.state is the same as reservations[].instances[].state
    Current implementation is reservations.instances.state = null
    jkamdjou
    @jkamdjou
    Hi all. Is it possible to force an error during query evaluation if a key doesn't exist?
    For example, jmespath.search("foo.bat.baz", {"foo": { "bar": { "baz": "foo" } } }) --> is it possible to surface that baz doesn't exist, opposed to a binary yes/no on the full match? If not, is there a good place to manually raise an error in the source if this behavior was desired (e.g. during ast evaluation, etc.)?
    Ryan Merolle
    @ryanmerolle
    {
       "third_parties": [
          {
             "name": "test",
             "speed": "1G",
             "circuit": false,
             "endpoints": [
                {
                   "switch": "switch1",
                   "interface": "Ethernet16",
                   "cross_connect_id": "AAA",
                   "ip": "10.191.10.170/31",
                   "peer_ip": "10.191.10.171"
                },
                {
                   "switch": "switch2",
                   "interface": "Ethernet16",
                   "cross_connect_id": "AAA",
                   "ip": "10.191.10.170/31",
                   "peer_ip": "10.191.10.171"
                }
             ],
             "outbound": [
                {
                   "name": "test-out",
                   "prefixes": [
                      "10.191.91.96/28"
                   ]
                }
             ],
             "inbound": [
                {
                   "name": "test-in",
                   "prefixes": [
                      "1.1.1.1/32"
                   ]
                }
             ]
          },
          {
             "name": "anothertest",
             "speed": "10G",
             "circuit": false,
             "endpoints": [
                {
                   "switch": "switch4",
                   "interface": "Ethernet16",
                   "cross_connect_id": "AAA",
                   "ip": "10.191.10.170/31",
                   "peer_ip": "10.191.10.171"
                },
                {
                   "switch": "switch2",
                   "interface": "Ethernet16",
                   "cross_connect_id": "AAA",
                   "ip": "10.191.10.170/31",
                   "peer_ip": "10.191.10.171"
                }
             ],
             "outbound": [
                {
                   "name": "test-out",
                   "prefixes": [
                      "10.191.91.96/28"
                   ]
                }
             ],
             "inbound": [
                {
                   "name": "test-in",
                   "prefixes": [
                      "1.1.1.1/32"
                   ]
                }
             ]
          }
       ]
    }
    how would I go about listing all third party dictionaries where at least 1 nested dictionary in endpoints switch=='switch1'
    I cannot wrap my head around it
    thanks!
    Chris Armstrong
    @chris-armstrong
    Try this expression: third_parties[?endpoints[?switch == 'switch1']]
    it took me a while too, but the basic logic is:
    • apply a filter expression to the third_parties array => third_parties[? ... ]
    • apply a filter expression to the endpoints array => third_parties[?endpoints[? ... ]]
    • match elements of the endpoints array that have switch == 'switch1' => third_parties[?endpoints[?switch=='switch']]
    • if the resulting endpoints array has a length > 0, it will match an element in the third_parties array
    Ryan Merolle
    @ryanmerolle
    thanks soo much!
    I am throw into my note pad
    Ryan Merolle
    @ryanmerolle
    ha now have to figure out how to replace switch1 in ansible with {{ inventory_hostname }}
    Alex Domoradov
    @alexeyd-itsoft
    {
        "nodes": {
            "http://10.0.7.1:8081": {
                "services": [{
                    "message": "OK",
                    "node_id": "itests-3eac-master",
                    "service_id": "srv1@123",
                    "state": "HEALTHY"
                }, {
                    "message": "OK",
                    "node_id": "itests-3eac-master",
                    "service_id": "srv2@123",
                    "state": "HEALTHY"
                }]
            },
            "http://10.0.7.2:8081": {
                "services": [{
                    "message": "OK",
                    "node_id": "itests-3eac-slave",
                    "service_id": "srv1@123",
                    "state": "HEALTHY"
                }, {
                    "message": "OK",
                    "node_id": "itests-3eac-slave",
                    "service_id": "srv2@123",
                    "state": "HEALTHY"
                }]
            }
        }
    }
    with the following json can't figure out how to get service_id fro the node that contains word master . Could anyone help me

    I have tried the following

    nodes.*.services[][?contains(node_id,'master')].service_id

    but without success

    Chris Armstrong
    @chris-armstrong
    @alexeyd-itsoft The behaviour here is a bit weird (after you flat map .services, I'd expect to be able to just filter the array, but there's something weird about it, possibly a hidden type conversion). I was able to get the correct results by doing piping the first result into another search:
    nodes.*.services[] | @[?contains(node_id, 'master')].service_id
    Chris Armstrong
    @chris-armstrong
    Ok, it's order of operations thing - the following achieves the right result too: (nodes.*.services[])[?contains(node_id, 'master')].service_id - the parse tree reveals the issue but I'm not familiar enough to understand what's happening
    Alex Domoradov
    @alexeyd-itsoft
    @chris-armstrong thx a lot for you help, I have a little bit modified your solution and got the following
    nodes.*.services[] | @[?contains(node_id, 'master')]  | @[?contains(service_id,'srv1')].service_id | @[0]
    now I get service id for specific service name
    zeus357
    @zeus357
    Hi all, is it possible to map the value of an element in the jmespath expression? for example, using the below data, if version=='1', then set version output as 'A'. similarly, if version=='2', then set version output as 'B'...etc. Any help would be appreciated.
    [
    {
    "type": "book",
    "title": "spiderman",
    "version": "1"
    },
    {
    "type": "dvd",
    "title": "spiderman",
    "version": "2"
    }
    ]
    Chris Armstrong
    @chris-armstrong
    From what I can see, the language wouldn't permit you to do this - there is map function and merge function, but you're not able to express index lookups using a variable, only a number literal.
    This is as close as I got, but this won't compile
    map(&merge(@, (`[{"version": "A"}, {"version": "B"}]`)[@.version]), @)
    zeus357
    @zeus357
    Thanks Chris
    anotherPassword
    @anotherPassword
    Any thoughts on this issue I am having? Query "instance.availableField[?startswith(name, 'ansible')]" Works in JMESPath tutorial but throws error in ansible tower:
    "msg": "JMESPathError in json_query filter plugin:\nIn function starts_with(), invalid type for value: LastBackupStatus-com.dellemc.avamar, expected one of: ['string'], received: \"unknown\"",
    "_ansible_no_log": false