Comments (7)
If you build from master, you can now do this:
del(.bar)
It's not documented yet, and the exact semantics of what happens when you replace .bar
with something more complicated will probably change, but that should solve the problem.
from jq.
Ah. You can in fact do this. First you need a means to recurse down to all of the children of a node, which you can do by defining a recursive function:
def all_children: . , (.data.children | select(. != null) | .[] | all_children);
Putting a node through all_children
returns the node itself and all child nodes. Then, to get all of the author
fields from any node in your dataset, you'd need:
.[] | all_children | .author
You can then delete all of the author fields with:
del(.[] | all_children | .author)
Defining a new recursive function each time you want to do this is effort, so I've added a recurse
function to the standard library, which is defined as:
def recurse(f): . , (f | select(. != null) | recurse(f));
The program to delete all author fields anywhere in the document now becomes:
del(.[] | recurse((.data.children // []) | .[]) | .author)
(foo // []
returns []
if foo
is null, or foo
otherwise.)
from jq.
Now I'm happier with what del(complicated_thing) does. You can del() all sorts of junk:
del(.foo,.bar,.baz)
deletes those three keysdel(.foo[0].bar.baz)
removes thebaz
key from the object at.foo[0].bar
del(.foo[0,1,2])
deletes the first three items of the array at field "foo"del(.foo[] | select(.score < 20))
deletes items in the array at field "foo" whose "score" field is below 20.
Not quite sure what you meant by "recursive", though.
from jq.
Yes, this is definitely a missing feature. Options include:
. - "bar"
. - ["bar"]
remove("bar")
.bar = empty
I think 1 or 2 might be what you suggested. I'm not sure which I prefer yet, but I'm kind of partial towards 4 since it fits with the current meanings of assignment and empty
, rather than adding a new construct.
from jq.
you're the domain expert, choose the most appropriate solution :)
from jq.
That's great dude. Is it recursive too ?
from jq.
By recursive, I mean that it's able to remove a node that repeats throughout a document structure. For example, Reddit's articles go fairly deep with repeating element/attr names:
from jq.
Related Issues (20)
- `match(...)` silently crashes when nothing is found HOT 3
- `scan("(a)(b)")` returning an array is not documented
- Unable to install jp using poetry or pip HOT 1
- fromdateiso8601 not inverse of todateiso8601 HOT 5
- The jq returned wrong json after updating based on select. HOT 1
- capture returns inconsistent results for optional named group HOT 2
- need command line options to print path and key values of JSON file HOT 7
- Please add nullish coalescing operator "??" to JQ? HOT 3
- Cannot select sub-field that has a dash in the name of the field HOT 1
- compile error HOT 10
- Multiplying -1 with 0 must result 0, not -0 HOT 2
- ~/.jq is not sourced on windows
- Unexpected output HOT 1
- Null bytes are handled inconsistently HOT 5
- tonumber doesn't work on `true` or `false` HOT 4
- "color for object keys" from JQ_COLORS doesn't seem to be respected HOT 4
- `range/3` behaviour when $init and $upto arguments are not numbers HOT 4
- Regular expression alternation (|) used with quantifier (* or +) returns inconsistent results when first alternative is able to match an empty string HOT 3
- Create Multi Smaller Files From a Big Json File HOT 5
- Add the Symbolic Binding Operator ("as") to the operator priority table HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jq.