jsonjq

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


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?


Solution

  • 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://jqlang.github.io/jq/manual/