Skip to content
Advertisement

jq script file not getting the key/value pair

I would like to put all jq filters inside a text file and use the jq -L option to execute the filters. However, I can’t get this simple thing working.

Inside my sample2.json file, I have:

{
        "a": 86,
        "b": 99
}

enter image description here

Inside my json2csv file, I have:

.["a"]

enter image description here

When I do:

cat sample.json | jq -L json2csv

The output is the whole sample.json file, like this:

enter image description here

but I would expect the output is only 86because:

enter image description here

How do I change the content of the json2csv file so I can only get the 86 in the output? I am using jq 1.6. Thanks!

Advertisement

Answer

Use jq -f json2csv instead.

From man jq:

-Ldirectory / -L directory:

Prepend directory to the search list for modules. If this option is used then no builtin search list is used. See the section on modules below.

Compare this to:

-f filename / –from-file filename:

Read filter from the file rather than from a command line, like awk´s -f option. You can also use # to make comments.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement