Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse error: Invalid numeric literal at line 2, column 0

Tags:

json

jq

i am trying to read for a big json data structure and I get the message:

parse error: Invalid numeric literal at line 2, column 0

The command that I'm using is the next one:

n_rules=$(echo rulebase_list | jq '.total')

and the file has in the first hierarchy level a variable which is

"total" : 126

Do you know why im experiencing problems with that? I suppose that the problem is that 126 is a numeric value but what can I do?

like image 387
unai abrisketa sanchez Avatar asked Nov 13 '18 11:11

unai abrisketa sanchez


2 Answers

Carefully check your script against the one you posted in the question. If they match then the answer is very easy.

There is no "total" : 126 in the string you pass to jq because you pass it the output of echo rulebase_list that is rulebase_list.

What you probably wanted is to send to jq the content of the rulebase_list file and the tool for this is cat:

n_rules=$(cat rulebase_list | jq '.total')

Alternatively (and faster) is to redirect the input of jq from the file:

n_rules=$(jq '.total' < rulebase_list)

Or to specify the input file name as the last argument in the command line of jq:

n_rules=$(jq '.total' rulebase_list)

Read more about jq: https://stedolan.github.io/jq/manual/

like image 119
axiac Avatar answered Nov 14 '22 13:11

axiac


You forgot to include $ in your script. Add it and it'll be fixed.

n_rules=$(echo $rulebase_list | jq '.total')
like image 1
Pritish Vaidya Avatar answered Nov 14 '22 11:11

Pritish Vaidya