Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More examples on Concatenating nested elements #80

Open
brokedba opened this issue Nov 29, 2020 · 1 comment
Open

More examples on Concatenating nested elements #80

brokedba opened this issue Nov 29, 2020 · 1 comment

Comments

@brokedba
Copy link

brokedba commented Nov 29, 2020

Hi,

I think it would be very nice to add an example on how to use a join to concatenate the values of two keys inside a nested list of key values.

Using the query syntax I tried to create tuples combining the values of name and value(see below json structure) but it took me a lot of time to figure that out as there was no specific example on the Jmespath official web doc (the online jmspath validators didn't help neither in my case) .

Anyway, in order to get that I had to run the below

 [*].capabilities[].join(':',[name,value])

below is the json source input

[
  {
    "apiVersions": null,
    "capabilities": [
      {
        "name": "MaxResourceVolumeMB",
        "value": "10240"
      },
      {
        "name": "OSVhdSizeMB",
        "value": "1047552"
      },
      {
        "name": "vCPUs",
        "value": "1"
      },
      {
        "name": "HyperVGenerations",
        "value": "V1"
      },
      {
        "name": "MemoryGB",
        "value": "2"
      },
      {
        "name": "MaxNetworkInterfaces",
        "value": "2"
      }
    ],
    "capacity": null,
    "costs": null,
    "family": "standardAv2Family",
    "kind": null,
    "locationInfo": [
      {
        "location": "eastus",
        "zoneDetails": [],
        "zones": [
          "3",
          "2",
          "1"
        ]
      }
    ],
    "locations": [
      "eastus"
    ],
    "name": "Standard_A1_v2",
    "resourceType": "virtualMachines",
    "restrictions": [],
    "size": "A1_v2",
    "tier": "Standard"
  },
..
}
]

the result will look like below :

[                                                             
  "MaxResourceVolumeMB:10240",                                
  "OSVhdSizeMB:1047552",                                      
  "vCPUs:1",                                                  
  "HyperVGenerations:V1",                                     
  "MemoryGB:2",                                                                                
  "MaxNetworkInterfaces:2"                                    
]                                                             

thanks

@christianblueskylabs
Copy link

christianblueskylabs commented Mar 27, 2024

Agreed on the examples, it was kinda lacking. I luckily bumped into OP's issue from a Google Search, and glad somebody also figured out this is a problem.

For those who aren't having much success replicating OP's example, this one worked for me:

[].join('=', [key, value])
[
  {
    "contentType": "",
    "etag": "YfyzmDll0F4gaqEU4ojfpkVjSLqQJSTs3eLpflzrdNg",
    "key": "coke",
    "label": null,
    "lastModified": "2024-03-27T06:07:36+00:00",
    "locked": false,
    "tags": {},
    "value": "no sugar"
  },
  {
    "contentType": "",
    "etag": "Ck3mZ96csO3OEQeRJamEbG0CYTyZ_XK4co8VxgVi4Jc",
    "key": "the_voice",
    "label": null,
    "lastModified": "2024-03-27T04:46:54+00:00",
    "locked": false,
    "tags": {},
    "value": "you're"
  }
]

Result:

[
  "coke=no sugar",
  "the_voice=you're"
]

Using the main website https://jmespath.org/ tester.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants