archan937 / jsonv.sh

A Bash command line tool for converting JSON to CSV
MIT License
217 stars 45 forks source link

Converting json with just one element #2

Open srrmlwn opened 8 years ago

srrmlwn commented 8 years ago

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.

paulz1 commented 7 years ago

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.

lrzuniga commented 6 years ago

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