Extracting data from JSON file

How do you extract data from JSON file? For XML file, "xmlstarlet" is the tool you try first. Is there similar tool for JSON format? I tried "jq" today, but its syntax is a bit weird from first look. -- William Park <opengeometry@yahoo.ca>

On Fri, Jul 19, 2019 at 03:40:29AM -0400, William Park via talk wrote:
How do you extract data from JSON file?
For XML file, "xmlstarlet" is the tool you try first. Is there similar tool for JSON format? I tried "jq" today, but its syntax is a bit weird from first look.
python: https://docs.python.org/3/library/json.html c: https://github.com/vincenthz/libjson https://github.com/json-c/json-c etc. There are lots. jq looks decent if you have some reason to want a command line tool for it. I haven't tried it. I have only ever wanted to deal with json data in an application. Fortunately. :) -- Len Sorensen

On Fri, Jul 19, 2019 at 01:06:05PM -0400, Lennart Sorensen via talk wrote:
On Fri, Jul 19, 2019 at 03:40:29AM -0400, William Park via talk wrote:
How do you extract data from JSON file?
For XML file, "xmlstarlet" is the tool you try first. Is there similar tool for JSON format? I tried "jq" today, but its syntax is a bit weird from first look.
python:
https://docs.python.org/3/library/json.html
c:
https://github.com/vincenthz/libjson https://github.com/json-c/json-c
etc. There are lots.
jq looks decent if you have some reason to want a command line tool for it. I haven't tried it. I have only ever wanted to deal with json data in an application. Fortunately. :)
I'm writing automated test scripts. Unfortunately for me, input configuration is XML, and I use "xmlstarlet" to get data from that. 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! -- William Park <opengeometry@yahoo.ca>

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

On Sat, Jul 20, 2019 at 09:56:53AM -0400, Stewart C. Russell via talk wrote:
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>
I'm trying to avoid doing this kind of thing, because I will be spending way too much time dealing with corner cases. I already have! That's why I'm looking for "proper" parser.
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.
Yeah, something I noticed. :-( -- William Park <opengeometry@yahoo.ca>

On Sat, Jul 20, 2019 at 09:56:53AM -0400, Stewart C. Russell via talk wrote:
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.
RFC7159 says "names within an object SHOULD be unique". So it isn't REQUIRED, but implementations are not required to do something you would like with duplicates and as you have seen most implementations use the last entry seen and doing it is very much not recommended. -- Len Sorensen

<snip>
I'm writing automated test scripts. Unfortunately for me, input configuration is XML, and I use "xmlstarlet" to get data from that. The output is JSON, and I need to extract data from that to say "Pass" or "Fail".
<snip> Hi William, I have attached 3 files. Put all 3 files in the same folder/directory and run using the terminal: 1 - python duplicates.py 2 - python duplicates2.py The file 'duplicates.py' will detect duplicates in json passed in as a 'string' form. The file 'duplicates2.py' will detect duplicates read from a disk file ( in this case the file william.json) I have no idea how comfortable you are with python but do go through all 3 files. it is not hard to understand. If you have questions please let me know. At least this will help you extract what you need. Hope this helps - Aruna

I use CPAN's JSON module [1] with Perl .. works great. Alex 1. https://metacpan.org/pod/JSON On Fri, Jul 19, 2019 at 3:40 AM William Park via talk <talk@gtalug.org> wrote:
How do you extract data from JSON file?
For XML file, "xmlstarlet" is the tool you try first. Is there similar tool for JSON format? I tried "jq" today, but its syntax is a bit weird from first look. -- William Park <opengeometry@yahoo.ca> --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
-- Alex Beamish Software Developer / https://ca.linkedin.com/in/alex-beamish-5111ba3 Speaker Wrangler / Toronto Perlmongers / http://to.pm.org/ Baritone, Operations Manager / Toronto Northern Lights, 2013 Champions / www.northernlightschorus.com
participants (5)
-
Alex Beamish
-
Aruna Hewapathirane
-
lsorense@csclub.uwaterloo.ca
-
Stewart C. Russell
-
William Park