Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two arrays (`keys` and `values`) into an object using JMESPath?

Tags:

jmespath

I have a JSON object with two arrays — one keys array and one values array, both of the same length. Using jmespath, I want to construct a new object using the values of the keys array as the keys and the values of the values array as the values, like array_combine in PHP.

For example, here's the input:

{
    "keys": [
        "a",
        "b",
        "c"
    ],
    "values": [
        1,
        2,
        3
    ]
}

And here is the output I'm expecting:

{
    "a": 1,
    "b": 2,
    "c": 3
}

Is there any built-in function to achieve this?

like image 447
jagwar3 Avatar asked Aug 09 '15 03:08

jagwar3


1 Answers

Unfortunately, it looks like this is not possible yet.

Github issue: jmespath.py#152 — (located in the repo of the Python implementation)

You would need the zip and from_items functions, proposed (!) for the specs in this github pull request.

like image 140
myrdd Avatar answered Nov 05 '22 00:11

myrdd