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)
- 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
- Including and importing module with importing JSON crashes jq
- The code example for trim ltrim and rtrim from the manual do not work HOT 2
- Incorrect `Invalid path expression` in destructuring expressions
- 1.7.1 dumps core on Cygwin, trying to find the input filename to print an error message HOT 5
- jq --raw modes, --binary and newlines
- Request for snap maintenance HOT 11
- strptime month does not match input HOT 2
- Using unique or unique_by in a filter causes subsequent (after comma ,) filter inputs to be replaced with its output HOT 5
- Docker images lack `latest` tag HOT 2
- modulemeta failure when module location is found by "search"
- oss-fuzz/issues/detail?id=68056
- Unexpected env builtin behaviour HOT 5
- Undefined behaviour in use of <ctype.h> functions
- Add builtin to output to file HOT 5
- Сreating multilevel json with the jq utility in a bash script HOT 2
- More helpful message when passing a JSON-SEQ file without the `--seq` flag HOT 1
- `JV_PRINTF_LIKE` macros in `jv.h` missing fallback definition 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.