Open ChenXiaoTemp opened 6 years ago
@ChenXiaoTemp have you being able to find a solution?
I would also be interested in this
I have a similar case.
+1 Interested as well!
This would be amazing
Something I would like to do as well.
That is something that I am also interested at...
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.
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.
Also looking for this functionality.
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", ... }
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.
So, will this be implemented in jmespath since it's added to kyverno over a 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:
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
}
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?
@apoorv22 Could you kindly delete this comment? It shows up in google searches and wastes time.
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!!