jsonv.sh icon indicating copy to clipboard operation
jsonv.sh copied to clipboard

Converting json with just one element

Open srrmlwn opened this issue 9 years ago • 2 comments

Thanks for this super helpful script!

I did realize however, a simple json with just one element such as the below doesn't return any output.

[
{
    "name": "Dagny Taggart",
    "id": 1,
    "age": 39
 }
]

Can this be checked please?

Thanks.

srrmlwn avatar Jan 21 '16 05:01 srrmlwn

I confirm, I have the same problem. It does not work with single json (json with only one record).

For the moment I have a little workaround. I'm working very rarely with AWK, so may be it's not very correct/good solution, but for the moment it works for me.

It seems that the problem is in the line 99 of jsonv file.

count=$(cat $tokens | sed 's/^[\["a-z,]*//g' | sed 's/,.*//g' | gawk '/^[0-9]+$/ && !_[$0]++' | gawk -F\t 'BEGIN{max==""} ($1 > max) {max=$1} END{print max}')

BEGIN{max==""} ($1 > max)

does not work if $1 is equal to 0.

I changed it for count=$(cat $tokens | sed 's/^[\["a-z,]*//g' | sed 's/,.*//g' | gawk '/^[0-9]+$/ && !_[$0]++' | gawk -F\t 'BEGIN{max=0} ($1 > max) {max=$1} END{print max}')

and it works with single json file. Hope it will be corrected properly by maintainer.

paulz1 avatar Dec 12 '16 16:12 paulz1

@paulz1 your change seems to work correctly. I'd suggest submitting a pull request.

lrzuniga avatar Dec 13 '17 15:12 lrzuniga