jmespath.py icon indicating copy to clipboard operation
jmespath.py copied to clipboard

Create object from a key-value list

Open ChenXiaoTemp opened this issue 7 years ago • 15 comments

Hello,

First and foremost, thanks in advance for sharing JMESPath. It is very powerful especially it is used in ansible, which makes it very simple when querying or creating specified data.

Recentlly, I faced a challenge that when I queried a object array, I want to extract some attributes from it and make these attributes to build a new object. Consider the case of a JSON document like:

[ {"name":"instance-1","ip":"10.0.0.1"}, {"name":"instance-2","ip":"10.0.0.2"}, {"name":"instance-3","ip":"10.0.0.3"} ]

Now, I want to build an object(Because I want to call some other modue in ansible which needs a new format) : { "instance-1":"10.0.0.1", "instance-2":"10.0.0.2", "instance-3":"10.0.0.3", }

How do I do ?

Thansk very much!!

ChenXiaoTemp avatar Nov 10 '17 10:11 ChenXiaoTemp

@ChenXiaoTemp have you being able to find a solution?

chaliy avatar Mar 12 '18 17:03 chaliy

I would also be interested in this

cytopia avatar May 24 '18 09:05 cytopia

I have a similar case.

allanlewis avatar Aug 08 '18 15:08 allanlewis

+1 Interested as well!

barlik avatar Aug 22 '18 08:08 barlik

This would be amazing

greg-at-symcor-dot-com avatar Mar 29 '19 16:03 greg-at-symcor-dot-com

Something I would like to do as well.

dlb8685 avatar Mar 31 '20 16:03 dlb8685

That is something that I am also interested at...

gabriel19913 avatar Jul 02 '20 01:07 gabriel19913

I don't think it can be done because key is defined by "identifier", not "expression" as can be seen in this snippet of the grammar.

image

What you want is an enhancement to the specification, not so much the python implementation. I'm not sure what github repo is for the specification, or how to go about that.

timharsch avatar Aug 20 '20 18:08 timharsch

Also looking for this functionality.

mrmedicine avatar Jan 19 '21 09:01 mrmedicine

if adding dynamic key names to multihashes is too complicated, a function that takes a list of key-value pairs to produce a hash might be easier to implement:

merge_kv( [ [ "k1", "v1" ], [ "k2", "v2" ], ... ]  )
merge_kv( [ { k: "k1", v: "v1" }, { k: "k2", v: "v2" }, ... ]  )

OUT: { k1: "v1", k2: "v2", ... }

thielj avatar Jul 31 '21 17:07 thielj

The fact that this is not doable in JMESPath while with jq I can just do:

map({ (.name): .ip }) | add

Is why I avoid JMESPath wherever I can.

jakubgs avatar Feb 07 '22 16:02 jakubgs

So, will this be implemented in jmespath since it's added to kyverno over a year ago?

filipopo avatar May 17 '23 13:05 filipopo

Funny how chatgpt provides a solution to this , which did not work when i tried it

Let's assume you have the following JSON data as input:

{
  "data": [
    { "name": "John", "age": 30 },
    { "name": "Alice", "age": 25 },
    { "name": "Bob", "age": 35 }
  ]
}

You want to create a new JSON object where the keys are the "name" values, and the values are the "age" values. You can use the following JMESPath expression to achieve this:

data | { @.name: @.age }

Here's how the expression works:

  • data selects the "data" array from the input JSON.
  • | (pipe) is used to apply the transformation to each element of the array.
  • { @.name: @.age } creates a JSON object where the key is taken from the "name" field of each element (@.name), and the value is taken from the "age" field of each element (@.age).

The result of applying this JMESPath expression to the input JSON data would be:

{
  "John": 30,
  "Alice": 25,
  "Bob": 35
}

apoorv22 avatar Sep 13 '23 13:09 apoorv22

For anyone arriving here from an internet search, the reply above pattern and explanation is AI nonsense and doesn't work.

I admit the "which did not work when I tried it" part should have been enough of a clue about that, but why post it at all then?

andrewjbates avatar Jun 20 '24 20:06 andrewjbates