From a9733c495abeba9b086b7be509631b17cac099b3 Mon Sep 17 00:00:00 2001 From: Samuel Larkin Date: Thu, 4 Jul 2024 09:05:21 -0400 Subject: [PATCH] doc(json): converting xml to json and making sure elements are arrays --- docs/json.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/json.md b/docs/json.md index a9d3a7a..5b67cbd 100644 --- a/docs/json.md +++ b/docs/json.md @@ -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) | .[]' +```