You are not logged in.

#1 2019-01-06 17:42:38

Jclifton44
Member
From: United States
Registered: 2017-07-07
Posts: 32

JSON bash parser

Hello,

I am creating a Bash parser. I have posted it previously.
https://bbs.archlinux.org/viewtopic.php?id=243077

I am interested in constructive criticism.

https://github.com/jclifton44/shell-scr … rseJSON.sh

... You are using line-oriented tools like grep and sed, that will just never prove reliable on JSON...

... You do not need to pipe grep to grep, just write a proper regex in the first place.  You do not need to pipe sed to sed, just learn how sed commands actually work...

For example, in line 158:

key=$(echo $key | sed "s/^\s*//" | sed "s/\s*$//")

Output from echo is piped to sed to remove leading whitespace. Then output from sed is piped to sed to remove trailing whitespace. The output from sed is assigned to the variable 'key.'

How can one use sed to remove leading and trailing whitespace with one sed command.


I'm making revisions now. I'm going to comb through the thread I originally posted and make changes. If you have any other suggestions they are welcome.


"... a high signal to noise ratio..."

Offline

#2 2019-01-06 17:43:11

Jclifton44
Member
From: United States
Registered: 2017-07-07
Posts: 32

Re: JSON bash parser

I'll start:

There are no comments.


"... a high signal to noise ratio..."

Offline

#3 2019-01-06 17:51:17

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 2,853

Re: JSON bash parser

Jclifton44 wrote:

I'll start:

There are no comments.

So after your previous thread was dustbinned for refusing to accept the critiques you asked for, you will open a new thread with the same content?

And your big realization is that your script just needs comments?


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#4 2019-01-06 17:54:07

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 21,112
Website

Re: JSON bash parser

The line-orient tools will not work on real json.  But your tool is limited to json-like content all entered on a single line (on the command line no less).  So that critique does not apply given the constraint of your tool to not work on multiline input.

http://www.grymoire.com/Unix/Sed.html
Sed commands can be strung together, e.g. `'s/^\s*//;s/\s*$//'` or you can use an anchor for the content, `s/^\s\([^\s]*\).*/\1/'`.  But really in this case sed serves no purpose at all:

key=$(echo $key)

Done, e.g:

$ key="   one two  "

$ echo "[$key]"
[   one two  ]

$ key=$(echo $key)

$ echo "[$key]"
[one two]

Of if you are using bash there are better ways too, but you continue to refuse to specify a shell.

EDIT: there is a "gotcha" with the above echo method.  However given the numerous assumptions you are already making that the input is pure and perfect conforming JSON (but all on one line) there's really no harm in the above.  EDIT 2: wait, I'll be ****, and asterisk is a valid JSON key name.  So the above echo method would not be wise unless you are using bash where you can use the relevant `shopt` command to protect yourself ... and if you are using bash, there are - again - easier methods to remove all whitespace from a variable.

Last edited by Trilby (2019-01-06 18:09:43)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2019-01-06 17:55:23

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,210

Re: JSON bash parser

I'd probably split things up in smaller bits with functions. But i'm more of a Go programmer than shell programmer

Offline

#6 2019-01-06 17:56:13

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 26,865
Website

Re: JSON bash parser

Jclifton44 wrote:

I'll start:

There are no comments.


That's not how this place works. You ask for feedback, and then you incorporate it when it is provided. You don't bump the thread with empty posts like it is failbook.

I'll leave this open for now but the next ill-advised post will see it dustbinned.


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#7 2019-01-06 19:19:18

Jclifton44
Member
From: United States
Registered: 2017-07-07
Posts: 32

Re: JSON bash parser

eschwartz wrote:
Jclifton44 wrote:

I'll start:

There are no comments.

So after your previous thread was dustbinned for refusing to accept the critiques you asked for, you will open a new thread with the same content?

And your big realization is that your script just needs comments?

Criticism is welcome. I'm not refusing to accept critique.


"... a high signal to noise ratio..."

Offline

#8 2019-01-06 19:26:57

Jclifton44
Member
From: United States
Registered: 2017-07-07
Posts: 32

Re: JSON bash parser

Trilby wrote:

Of if you are using bash there are better ways too, but you continue to refuse to specify a shell.

I'm using /bin/bash.


Also, it's been updated to parser numbers and boolean values for objects only. Key mistmatch errors and array out of bounds errors exit with status code 1 and a message.

Last edited by Jclifton44 (2019-01-06 19:31:02)


"... a high signal to noise ratio..."

Offline

#9 2019-01-06 19:37:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 21,112
Website

Re: JSON bash parser

Jclifton44 wrote:

I'm using /bin/bash.

You may use bash as the default shell on your system, but your script still is not.

But if you want to specify bash, then you can use bash's string manipulation to easily trim whitespace from the start and end of the string.

Last edited by Trilby (2019-01-06 19:39:49)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2019-01-06 19:45:20

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 2,853

Re: JSON bash parser

Trilby wrote:

EDIT: there is a "gotcha" with the above echo method.  However given the numerous assumptions you are already making that the input is pure and perfect conforming JSON (but all on one line) there's really no harm in the above.  EDIT 2: wait, I'll be ****, and asterisk is a valid JSON key name.  So the above echo method would not be wise unless you are using bash where you can use the relevant `shopt` command to protect yourself ... and if you are using bash, there are - again - easier methods to remove all whitespace from a variable.

it's far, far worse. The script already uses numerous bashisms that cause syntax errors if /bin/sh is not a symlink to bash, and on top of that, when piping `echo $key | sed ...` the glob evaluation is already being performed in the script right now.

The sed is redundant because it will never be utilized in the first place, unless the script is changed to protect itself from variable re-expansion by quoting everything.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

Board footer

Powered by FluxBB