Count the Number of Fields in JSON with jq

Sometimes when working with JSON on the command line, it is helpful to know how many fields exist in each document. Fortunately this is easy to do with jq. jq is a lightweight, easy to use, command line JSON processor.

Counting Fields in single JSON String

If you want to test individual JSON strings, you can echo these and pipe the output to jq:

Counting JSON Fields from File

If you are working with JSON records in a file you can simply pass the file path as a argument to jq. This will print the number of fields in each JSON record in the file.

If it is expected that all JSON records to have the same number of fields, you can simply print the first output line:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">