[GTALUG] Extracting data from JSON file
Stewart C. Russell
scruss at gmail.com
Sat Jul 20 09:56:53 EDT 2019
Hi William,
> I'm writing automated test scripts. Unfortunately for me, input
> configuration is XML, and I use "xmlstarlet" to get data from that.
Another one for the XML side is Dan Egnor's xml2. It converts XML to a
simple flat line-oriented format that works well with command line tools:
Trivial XML:
<xml>
<blort>squarnk</blort>
<mlem>-8</mlem>
</xml>
Examples:
$ xml2 < triv.xml
/xml/blort=squarnk
/xml/mlem=-8
$ xml2 < triv.xml | grep blort | 2xml
<xml><blort>squarnk</blort></xml>
Debian packages is "xml2". It would be nice if it had a json filter.
Note that the docs will likely have a bad link. The last docs I've seen
are here:
https://web.archive.org/web/20160812034655/http://dan.egnor.name/xml2/ref
xml2 is a bit old and makes some assumptions about input files that may
not be ideal. xmlstarlet may be the one to stick with.
> The output is JSON, and I need to extract data from that to say
> "Pass" or "Fail".
> Looks like "jq" is the answer. But, I find that "jq" ignores
> duplicate keys, and accepts only the last one. Major pain!
Wait, are you saying that the JSON has duplicate hash keys, like this:
{"blort":"squarnk","mlem":-8,"blort":"phweeen"}
(so, two values for the key "blort")? It really shouldn't do that, even
though the file is valid JSON. Most JSON parsers only return the last value.
cheers,
Stewart
More information about the talk
mailing list