-
Notifications
You must be signed in to change notification settings - Fork 182
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
Create object from a key-value list #152
Comments
@ChenXiaoTemp have you being able to find a solution? |
I would also be interested in this |
I have a similar case. |
+1 |
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:
OUT: |
The fact that this is not doable in JMESPath while with 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:
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
} |
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!!
The text was updated successfully, but these errors were encountered: