xml to json | mac command line *

https://www.howtogeek.com/devops/how-to-convert-xml-to-json-on-the-command-line/

Use the xq Utility

You’ll want to use a custom made utility for this, rather than trying to parse it with something like regex, which is a bad idea. There’s a utility called xq that is perfect for this task. It’s installed alongside yq, which works for YAML. You can install yq from pip – or home-brew:

pip install yq

Under the hood, this utility uses jq to handle working with JSON, so you’ll need to download the binary, and move it to somewhere on your PATH (/usr/local/bin/ should work fine).

Now, you’ll be able to parse XML input by piping it into xq:

cat xml | xq .

cat filename.xml | xq .

The . operator means you want to convert all of the XML to JSON. You can actually use full jq syntax here to select sub-elements, which you can read our guide on.

You can also output xq‘s response as XML with the -x flag:

xq -x

This lets you use jq‘s selection syntax to parse XML while keeping it in XML form. Though it doesn’t seem to be able to convert the other way, as xq still wants XML syntax in.

Scroll to Top