Skip to content
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

Open
ChenXiaoTemp opened this issue Nov 10, 2017 · 15 comments
Open

Create object from a key-value list #152

ChenXiaoTemp opened this issue Nov 10, 2017 · 15 comments

Comments

@ChenXiaoTemp
Copy link

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
Copy link

chaliy commented Mar 12, 2018

@ChenXiaoTemp have you being able to find a solution?

@cytopia
Copy link

cytopia commented May 24, 2018

I would also be interested in this

@allanlewis
Copy link

I have a similar case.

@barlik
Copy link

barlik commented Aug 22, 2018

+1
Interested as well!

@greg-at-symcor-dot-com
Copy link

This would be amazing

@dlb8685
Copy link

dlb8685 commented Mar 31, 2020

Something I would like to do as well.

@gabriel19913
Copy link

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

@timharsch
Copy link

timharsch commented Aug 20, 2020

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
Copy link

Also looking for this functionality.

@thielj
Copy link

thielj commented Jul 31, 2021

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
Copy link

jakubgs commented Feb 7, 2022

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
Copy link

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

@apoorv22
Copy link

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
}

@andrewjbates
Copy link

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
Copy link

liath commented Sep 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests