Adopt jshn tool to parse json from shell script

251 views
Skip to first unread message

Sergey Ponomarev

unread,
May 9, 2021, 6:57:03 AM5/9/21
to json-c
Sorry that I'm writing not about json-c itself but this is related.
Currently it's very inconvenient to work with JSON from shell scripts.
From what I saw the most popular way is just to install jq or any other tool and call it.
The dependency is quite heavy and not installed by default on most platforms.
But it's still extremely hard to generate JSON with hierarchy.

The solution is exists in OpenWRT Linux distro that is used for routers with extremely small memory. It's called jshn and this is a command line tool that uses json-c and generates a shell script that populates json structure as hierarchy of shell variables and provides a shell script that can be included as a library to your shell script:


#!/bin/sh
 
# source jshn shell library
. /usr/share/libubox/jshn.sh
 
# generating json data
json_init
json_add_string "msg" "Hello, world!"
json_add_object "test"
json_add_int "testdata" "1"
json_close_object
MSG=`json_dump`
# MSG now contains: { "msg": "Hello, world!", "test": { "testdata": 1 } }
 

The tool/library is old, stable, well tested, easy to use and works perfectly.
On my working project I working a lot with scripts that runed in Ubuntu based docker containers and I have to use some Python scripts that in OpenWRT can be easily written in plain bash with the jsnh tool.
So I ported the jshn to Ubuntu https://github.com/stokito/jshn-jsonc
The original jshn used some OpenWRT specific code to work with map/dict (AVL tree)
I replaced it with linkhash.c from json-c and now the json-c is the only dependency to the jshn.
But in comments said that the linkhash.c is not for a public usage and may be changed without notice.
Another problem is that now I need to add this jshn package to all distros but I don't have any experience and time for this. And I'm not a C developer and can't make a good support.
So I would like to ask you to adopt my jshn version under https://github.com/json-c organization umbrella.
If the linkhash will ever update ten you adjust changes in the jshn too to keep binary compatibility so when json-c will be updated the jshn will be updated too and users will be safe.
Also you can easily create the needed packages for other distros.

I know, this is sounds like I want to drop my work to you but I hope that this will be a good companion project fro json-c and they should be together.

Eric

unread,
May 9, 2021, 12:08:29 PM5/9/21
to json-c
On Sunday, May 9, 2021 at 6:57:03 AM UTC-4 sto...@gmail.com wrote:
The solution is exists in OpenWRT Linux distro that is used for routers with extremely small memory. It's called jshn and this is a command line tool that uses json-c and generates a shell script that populates json structure as hierarchy of shell variables and provides a shell script that can be included as a library to your shell script:

hmm... seems like a rather awkward approach, but if it works I guess it's must be useful.
Thanks for the pointer.

> So I would like to ask you to adopt my jshn version under https://github.com/json-c organization umbrella.
 
I would encourage you to submit your changes upstream to the original jshn project instead.  IMO it's better to avoid forking off a project like this just to get it to build on another platform.  Though jshn might _use_ json-c, neither I nor afaik anyone else that's worked on json-c has any particular expertise with jshn, so it doesn't seem like it would make much sense to try to move it over.  (unless, perhaps, the existing jshn maintainer(s) want to move it over, and pull from a new location under github/json-c?)

The tool/library is old, stable, well tested, easy to use and works perfectly.
On my working project I working a lot with scripts that runed in Ubuntu based docker containers and I have to use some Python scripts that in OpenWRT can be easily written in plain bash with the jsnh tool.
So I ported the jshn to Ubuntu https://github.com/stokito/jshn-jsonc
The original jshn used some OpenWRT specific code to work with map/dict (AVL tree)
I replaced it with linkhash.c from json-c and now the json-c is the only dependency to the jshn.
But in comments said that the linkhash.c is not for a public usage and may be changed without notice.

You could just use it as-is for now, and if we end up changing the visibility of the linkhash functions in json-c, then simply copy the code you need into your project.
Though it seems a bit silly to duplicate it, you're really not using a whole lot of code.
Alternately, and what's probably a better option, is keeping the existing code that uses AVL tree functions as is, and provide a way to use alternate implementation (or include one) for platforms other than OpenWRT.  After all, AVL trees aren't OpenWRT specific, and there seem to be plenty of implementation options for other platforms.  Taking this approach will likely also help have your changes accepted upstream.

Another problem is that now I need to add this jshn package to all distros but I don't have any experience and time for this. And I'm not a C developer and can't make a good support.
 
I haven't done anything to get any package into any distros, so I'm not going to be much help here either, sorry.  (All packaging of json-c into various distros has been done by other people)

Eric

Sergey Ponomarev

unread,
May 9, 2021, 1:47:58 PM5/9/21
to jso...@googlegroups.com
Ok, thank you Eric for your points. I’ll try to copy avl map and publish package myself. 
Thank you for your work!

--
You received this message because you are subscribed to a topic in the Google Groups "json-c" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/json-c/iaI9Z-9_mOA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to json-c+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/json-c/3559b2f0-a535-4bf3-a6f6-ac4439311211n%40googlegroups.com.
--
Reply all
Reply to author
Forward
0 new messages