Skip to content

Commit

Permalink
doc(json): converting xml to json and making sure elements are arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelLarkin committed Jul 4, 2024
1 parent 00a38c8 commit a9733c4
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,29 @@ paste \
"eng": "\"[Life] is extremely difficult."
}
```


## XML to json

Using [yq](https://github.com/mikefarah/yq/), we can convert a xml document into a json file.

```sh
yq -p xml -o json < input.xml > output.json
```

## Convert to Array

Given

```json
[
{ "seg": ["A", "B"] },
{ "seg": "C"},
]
```

The second object is NOT an array but you need it to be an array to process all elements the same way, you can make sure all segments are arrays by doing:

```sh
jq '.[] | .seg | (if type == "object" then [.] else . end) | .[]'
```

0 comments on commit a9733c4

Please sign in to comment.