Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    I think you can set you expression in double quote and escape only the double quote
    no?
    omrishilton
    @omrishilton
    ill give it a try in a few minutes, thanks a lot for the help!
    jdevillard
    @jdevillard
    You’re welcome !
    hell-racer
    @hell-racer
    Hi guys! How do I accomplish simple value mapping? E.g. I have an input like [ { "a": "foo" }, { "a": "bar" } ] and I need output [ { "a": "123" }, { "a": "456" } ], where mappings foo -> 123 and bar -> 456 are hardcoded into expression.
    hell-racer
    @hell-racer
    I figured that out, thanks :)
    esultanza
    @esultanza
    hey! Is it possible to get all nested keys from the array?
    E.g.: documents[].passport.foreign, family[].children.daughter ...
    I know only how to get first level keys: keys(@). It will be documents, children.
    Amine Bouassida
    @amine-bouassida
    hello! I am using the azure cli, with a command that returns either a single json object or an array of objects with the same struct, and I couldn't figure out a single expression that would give me similar results in both cases. This is the expression I'm using: [][networkProfile.networkInterfaces[0].id, storageProfile.osDisk.managedDisk.id]
    this works if the json is an array, and the same, minus the [] at the beginning works on a single object
    I'm not that familiar with jmespath, but I couldn't figure out how to make the [] at the beginning optional..
    Amine Bouassida
    @amine-bouassida
    this very verbose expression works though: [networkProfile.networkInterfaces[0].id||[].networkProfile.networkInterfaces[0].id, storageProfile.osDisk.managedDisk.id||[].storageProfile.osDisk.managedDisk.id]
    hell-racer
    @hell-racer
    Hi Amine! Try to use the following prefix: '[@] | [] | [0] |'. E.g. in your case: [@] | [] | [0] | networkProfile.networkInterfaces[0].id. It should work as follows: 1. [@] - Wrap whatever input you have into an array (if it's already an array it will become nested array). 2. [] - Flatten the array (if the input was not an array this will have no effect). 3. [0] - Get the first element. 4. Get whatever you specify from the that first element.
    hell-racer
    @hell-racer
    If you get an array as input, and you need all the elements, try to use: [@] [] | [[*].networkProfile.networkInterfaces[0].id, [*].storageProfile.osDisk.managedDisk.id] []
    Krazy Bug
    @Krazybug
    Hi, I just discovered JMESpath and love its cohesive syntax compared to jq. However, I often use the "match" function with regexps. Is there something similar or a way to provide extend the syntax with function in the different implementations ?
    Joe Meree
    @joemeree

    Hi Folks! New guy. I'm working on a COVID project and I need some help extracting JSON.
    import json
    import jmespath
    filename = "FHIRBundle.json" # it's here https://datarobot.box.com/s/uri1d94laswjyju20pprttb6u95qh3hr
    with open(filename, 'r') as h:
    doc = json.load(h)

    i want to grab the first "entry" that is an "Observation"

    obs = jmespath.search("entry[?resource.resourceType=='Observation']", doc)
    print (obs)

    I'm getting an invalid JSON document with this. How to I get the whole entry element?

    I want to get this code.coding[0].code inside of obs

    :| didn't know the comments would do that...
    I want to grab the first "entry" that is an "Observation"
    I'm getting an invalid JSON document with this. How to I get the whole entry element?
    I want to get this code.coding[0].code inside of obs
    hell-racer
    @hell-racer
    Hi! The link shows py file, not json
    Joe Meree
    @joemeree
    hell-racer
    @hell-racer
    Your expression extracts valid json, it's just an array at the top
    you can get first element if you want (by using [0])
    e.g. entry[?resource.resourceType=='Observation'] | [0]
    to get that coding thing :) pls try entry[?resource.resourceType=='Observation'] | [0].resource.code.coding[0].code
    hell-racer
    @hell-racer
    Play with it using online evaluator at https://jmespath.org/, it's easier
    Joe Meree
    @joemeree
    Thank you Sir. This worked:
    obsroot = "entry[?resource.resourceType=='Observation'] | [0]."
    code = jmespath.search(obsroot + "resource.code.coding[0].code", doc)
    print (code)
    This seems clunky. Is there a cleaner way to extract the Observation and then query that extraction? (I have to do a lot of these...)
    hell-racer
    @hell-racer
    So you need whole entry and then extract various items from it?
    Joe Meree
    @joemeree
    Yes.
    hell-racer
    @hell-racer
    You can get the entry with entry[?resource.resourceType=='Observation'] | [0] and then extract what you need from there (resource.code.coding[0].code). Alternatively you can construct an object with properties you need.
    Joe Meree
    @joemeree
    I'll be getting many different documents (FHIR is a medical standard with many versions)
    This is good enough. I'll polish it up later THANKS!! (Monday deadline :) )
    hell-racer
    @hell-racer
    Good luck :)
    hell-racer
    @hell-racer

    Hi guys! How do i filter an array which is a property of outer object by another property of that object? E.g. given the predefined example at jmespath.org, I add "filter": "WA" to the object and then I try to change the expression so it uses "filter" instead of "WA".

    E.g. source data:

    {
      "filter": "WA",
      "locations": [
        {"name": "Seattle", "state": "WA"},
        {"name": "New York", "state": "NY"},
        {"name": "Bellevue", "state": "WA"},
        {"name": "Olympia", "state": "WA"}
      ]
    }

    ...and the expression:

    locations[?state == filter].name | sort(@) | {WashingtonCities: join(', ', @)}

    ...which doesn't work because if tries to use filter field inside location object. How do I change that to make it work?

    Anand Dasari
    @akdasari
    somebody help me how to get first level value based on the condition from child key
    in JMespath
    i have file like this { "value": [ { "id": "/subscriptions/xxxxxx6/resourceGroups/test-rg/providers/Microsoft.ContainerRegistry/registries/test/providers/Microsoft.Security/assessments/xxxx", "name": "xxxxxxxxxxxxxx", "properties": { "additionalData": { "subAssessmentsLink": "/subscriptions/xxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.ContainerRegistry/registries/test/providers/Microsoft.Security/assessments/xxxxx/subAssessments" }, "displayName": "Vulnerabilities in Azure Container Registry images should be remediated (powered by Qualys)", }
    based on "displayName" value i want to extract the "name" value
    hell-racer
    @hell-racer
    So like "if displayName = X then return name, otherwise null", right?
    Anand Dasari
    @akdasari
    its's an array i gav only one element for now . i always have different diplayname valye... right now i want to check if this displayname i want the value of the above "name" field value
    hell-racer
    @hell-racer
    So you need all "names" from the collection which "displayNames" = X?
    Anand Dasari
    @akdasari
    it's only single name element, sorry if i was vague
    in this exmple if displayName="Vulnerabilities....." then print "Jackson" ,,, which is a name
    i have tried this value[*].{name:name,display:properties.displayName}[?contains('Vulnerabilities in Azure Container Registry images should be remediated (powered by Qualys)')]
    but that gives both name and displayname, i want only name to be as a result to print
    hell-racer
    @hell-racer
    add [].name in the end?
    maybe |[].name
    I cannot get your example to work to test this myself :)
    Anand Dasari
    @akdasari
    wow fantastic, that does the trick adding at end
    thanks @hell-racer
    hell-racer
    @hell-racer
    Nice :D