Skip to main content
added 414 characters in body
Source Link
Kusalananda
  • 356.3k
  • 42
  • 737
  • 1.1k

jq is usually my go-to tool for any JSON processing, and Chris Davies is giving a good example of jq applied to this particular issue in their answer.

Another tool, called jo, is made specifically for constructing JSON on the command line. With jo, the JSON array can be had as the string $deps from the following short shell code:

deps=$( jo -a <Dependencies )

Adding an Infra as a final element in the array can be done like so:

deps=$( jo -a <Dependencies | jo -f - "Infra" )

With the data given in the question, this would produce the following JSON array as a string in $deps:

["Accounts","Aggregates","Blog","Configuration","Contacts","Contents","DataTypes","Forms","Geo","Globalization","Media","Modules","Navigation","Podcast","Seo","Social","Taxonomy","Tracking","Vlog","Infra"]

Add -p to the last invocation of jo to generate a pretty-printed array.


Please also be aware of some of the limitations of this tool. The following is a comment from Stéphane Chazelas:

Beware it treats number-looking strings as numbers, empty strings as null, [...] as arrays, @file would load the contents of a file, etc (see for instance printf '%s\n' 1e002 @/etc/shadow @/dev/watchdog 0xDEADBEEF '' '{}' | jo -a). It can't be used that way for arbitrary strings.

jq is usually my go-to tool for any JSON processing, and Chris Davies is giving a good example of jq applied to this particular issue in their answer.

Another tool, called jo, is made specifically for constructing JSON on the command line. With jo, the JSON array can be had as the string $deps from the following short shell code:

deps=$( jo -a <Dependencies )

Adding an Infra as a final element in the array can be done like so:

deps=$( jo -a <Dependencies | jo -f - "Infra" )

With the data given in the question, this would produce the following JSON array as a string in $deps:

["Accounts","Aggregates","Blog","Configuration","Contacts","Contents","DataTypes","Forms","Geo","Globalization","Media","Modules","Navigation","Podcast","Seo","Social","Taxonomy","Tracking","Vlog","Infra"]

Add -p to the last invocation of jo to generate a pretty-printed array.

jq is usually my go-to tool for any JSON processing, and Chris Davies is giving a good example of jq applied to this particular issue in their answer.

Another tool, called jo, is made specifically for constructing JSON on the command line. With jo, the JSON array can be had as the string $deps from the following short shell code:

deps=$( jo -a <Dependencies )

Adding an Infra as a final element in the array can be done like so:

deps=$( jo -a <Dependencies | jo -f - "Infra" )

With the data given in the question, this would produce the following JSON array as a string in $deps:

["Accounts","Aggregates","Blog","Configuration","Contacts","Contents","DataTypes","Forms","Geo","Globalization","Media","Modules","Navigation","Podcast","Seo","Social","Taxonomy","Tracking","Vlog","Infra"]

Add -p to the last invocation of jo to generate a pretty-printed array.


Please also be aware of some of the limitations of this tool. The following is a comment from Stéphane Chazelas:

Beware it treats number-looking strings as numbers, empty strings as null, [...] as arrays, @file would load the contents of a file, etc (see for instance printf '%s\n' 1e002 @/etc/shadow @/dev/watchdog 0xDEADBEEF '' '{}' | jo -a). It can't be used that way for arbitrary strings.

Source Link
Kusalananda
  • 356.3k
  • 42
  • 737
  • 1.1k

jq is usually my go-to tool for any JSON processing, and Chris Davies is giving a good example of jq applied to this particular issue in their answer.

Another tool, called jo, is made specifically for constructing JSON on the command line. With jo, the JSON array can be had as the string $deps from the following short shell code:

deps=$( jo -a <Dependencies )

Adding an Infra as a final element in the array can be done like so:

deps=$( jo -a <Dependencies | jo -f - "Infra" )

With the data given in the question, this would produce the following JSON array as a string in $deps:

["Accounts","Aggregates","Blog","Configuration","Contacts","Contents","DataTypes","Forms","Geo","Globalization","Media","Modules","Navigation","Podcast","Seo","Social","Taxonomy","Tracking","Vlog","Infra"]

Add -p to the last invocation of jo to generate a pretty-printed array.