jmespath / jmespath.py

JMESPath is a query language for JSON.
http://jmespath.org
MIT License
2.19k stars 181 forks source link

Create object from a key-value list #152

Open ChenXiaoTemp opened 6 years ago

ChenXiaoTemp commented 6 years ago

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!!

chaliy commented 6 years ago

@ChenXiaoTemp have you being able to find a solution?

cytopia commented 6 years ago

I would also be interested in this

allanlewis commented 6 years ago

I have a similar case.

barlik commented 6 years ago

+1 Interested as well!

greg-at-symcor-dot-com commented 5 years ago

This would be amazing

dlb8685 commented 4 years ago

Something I would like to do as well.

gabriel19913 commented 4 years ago

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

timharsch commented 4 years ago

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.

mrmedicine commented 3 years ago

Also looking for this functionality.

thielj commented 3 years ago

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", ... }

jakubgs commented 2 years ago

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.

filipopo commented 1 year ago

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

apoorv22 commented 1 year ago

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:

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

{
  "John": 30,
  "Alice": 25,
  "Bob": 35
}
andrewjbates commented 4 months ago

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?

liath commented 1 month ago

@apoorv22 Could you kindly delete this comment? It shows up in google searches and wastes time.