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

Ability to set and delete based on a jmespath #121

Open
smilledge opened this issue Feb 18, 2017 · 6 comments
Open

Ability to set and delete based on a jmespath #121

smilledge opened this issue Feb 18, 2017 · 6 comments

Comments

@smilledge
Copy link

For example if I had following dict;

data = {
    "foo": {
        "bar": [
            {"name": "one"}, 
            {"name": "two"}
        ]
    }
}

I'd like to be able to change the value of "one" to "three" be using something like;

new_data = jmespath.replace('foo.bar[1].name', 'three', data)

And then remove the first item of the "bar" list;

new_data = jmespath.remove('foo.bar[1]', data)
@bitdivine
Copy link

I too would be interested in deletion but rather as part of the language. Something like this:

jmespath.search('foo.bar | delete(name)', data)

The above would (rather unexcitingly) produce two empty dictionaries. However the potential is exciting - if those dictionaries had started out with all manner of other keys those other keys would be preserved!

@bitdivine
Copy link

I note that there is some discussion about having a to_items() function that converts a dictionary into a list of key,value pairs. If that happens, and there is presumably also a matching from_items(), then what you are asking could be done with to_items(@) | [?[0]!='bar'] | from_items(@). However we are talking about a hypothetical future.

@xeor
Copy link

xeor commented May 8, 2019

Did anything happen to this? We are needing to look into using a jq binding instead entirely because this is missing :(

@kvancamp
Copy link

I was also looking for this ability, but it may not fit well with jmespath. I'm thinking glom might be a better alternative: https://glom.readthedocs.io/en/latest/

@jiasli
Copy link

jiasli commented Mar 2, 2020

This is a feature great to have. Currently have to pipe to jq as a workaround.

Given the input

{
    "foo": {
        "bar": [
            {
                "name": "one"
            },
            {
                "name": "two"
            }
        ]
    }
}

To set: .foo.bar[1].name="three"

{
  "foo": {
    "bar": [
      {
        "name": "one"
      },
      {
        "name": "three"
      }
    ]
  }
}

To delete: del(.foo.bar[1])

{
  "foo": {
    "bar": [
      {
        "name": "one"
      }
    ]
  }
}

@caffeinatedMike
Copy link

Can this be considered for any upcoming improvements?

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

No branches or pull requests

7 participants