jmespath.py
jmespath.py copied to clipboard
Ability to set and delete based on a jmespath
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)
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!
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.
Did anything happen to this? We are needing to look into using a jq binding instead entirely because this is missing :(
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/
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"
}
]
}
}
Can this be considered for any upcoming improvements?