Menu

Parsing JSON

When you query a wiki page with the Allura REST API, you receive a JSON representation of the requested page. To parse json from a shell I could find two good alternatives: jq and the Python json library.

I did some initial tests and I found a problem with jq getting the markdown text. The default output looked OK, but the raw output, that I actually needed, looked like gibberish. With Python I immediately got the right output, so I decided to stick with Python.

Also with Python you can get it on a single line command, but obviously it is more complex. Still to get a string or a list of strings is quite easy.

$ curl -s -k -X GET \
> https://sourceforge.net/rest/p/demo-project/wiki/Project%20Web%20Services-Draft/ \
> | jq -r '.text'
$ curl -s -k -X GET \
> https://sourceforge.net/rest/p/demo-project/wiki/Project%20Web%20Services-Draft/ \
> | python -c "import sys, json; print(json.load(sys.stdin)['text'])"

The list of attachments is a list objects with key-value pairs for URL and size. I wanted to retrieve a list of URLs. This is really very simple with jq and I had a hard time finding the solution for python. But that was mainly because I was expecting a short Python notation for this, while I really had to add an extra step.

$ curl -s -k -X GET \
> https://sourceforge.net/rest/p/demo-project/wiki/Project%20Web%20Services-Draft/ \
> | jq -r '.attachments[].url'
$ curl -s -k -X GET \
> https://sourceforge.net/rest/p/demo-project/wiki/Project%20Web%20Services-Draft/ \
> | python -c "import sys, json; \
> attachments = json.load(sys.stdin)['attachments']; \
> print('\n'.join([a['url'] for a in attachments]))"

While writing this blog I was not able to reproduce the problematic jq behaviour with regard to getting the markdown text. I probably made some error in the notation, because now it seems to work fine. So I am switching back to jq in the script.

Posted by Henk van den Akker 2022-07-02

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.