Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
As stated elsewhere on this page, walk traverses bottom-up. To prevent new string leaves created by join from cascading further up, I'd go with a top-down approach using the recursive descent opera...
#2: Post edited
As stated elsewhere on this page, `walk` traverses bottom-up. To prevent new string leaves created by `join` from cascading up, I'd go with a top-down approach using the [recursive descent](https://jqlang.org/manual/#recursive-descent) operator `..` instead. For example,- ```jq
- (.. | arrays) |= (select(any(iterables)) // join(""))
- ```
- applied to
- ```json
[{"foo": "12"}, {"bar": [{"baz": "foobaz"}]}]- ```
- produces
- ```json
- [{"foo":"12"},{"bar":[{"baz":"foobaz"}]}]
- ```
- while
- ```json
- [[1, 2], [[3, 4], [[5, 6], [7]]]]
- ```
- becomes
- ```json
- ["12",["34",["56","7"]]]
- ```
- Note: Since `join` on numbers requires jq 1.6+, `join("")` could be replaced with `(map(tostring) | add)` to lower the requirements down to jq 1.5+.
- As stated elsewhere on this page, `walk` traverses bottom-up. To prevent new string leaves created by `join` from cascading further up, I'd go with a top-down approach using the [recursive descent](https://jqlang.org/manual/#recursive-descent) operator `..` instead. For example,
- ```jq
- (.. | arrays) |= (select(any(iterables)) // join(""))
- ```
- applied to
- ```json
- [{"foo": [1, 2]}, {"bar": [{"baz": ["foo", "baz"]}]}]
- ```
- produces
- ```json
- [{"foo":"12"},{"bar":[{"baz":"foobaz"}]}]
- ```
- while
- ```json
- [[1, 2], [[3, 4], [[5, 6], [7]]]]
- ```
- becomes
- ```json
- ["12",["34",["56","7"]]]
- ```
- Note: Since `join` on numbers requires jq 1.6+, `join("")` could be replaced with `(map(tostring) | add)` to lower the requirements down to jq 1.5+.
#1: Initial revision
As stated elsewhere on this page, `walk` traverses bottom-up. To prevent new string leaves created by `join` from cascading up, I'd go with a top-down approach using the [recursive descent](https://jqlang.org/manual/#recursive-descent) operator `..` instead. For example,
```jq
(.. | arrays) |= (select(any(iterables)) // join(""))
```
applied to
```json
[{"foo": "12"}, {"bar": [{"baz": "foobaz"}]}]
```
produces
```json
[{"foo":"12"},{"bar":[{"baz":"foobaz"}]}]
```
while
```json
[[1, 2], [[3, 4], [[5, 6], [7]]]]
```
becomes
```json
["12",["34",["56","7"]]]
```
Note: Since `join` on numbers requires jq 1.6+, `join("")` could be replaced with `(map(tostring) | add)` to lower the requirements down to jq 1.5+.
