Skip to main content
make horizontal scrolling unnecessary, other additions
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

This answer assumes that your filenames (or whatever text you wish to JSON encode) is valid UTF-8.

Two alternatives:

Not using xargs: Call jq with the pathnames as positional parameters directly from find with -exec. Read the found pathnames with --args and access them as the array $ARGS.positional[]positional in the jq expression. For each pathname, create a JSON object.

find "$HOME" -maxdepth 1 -type d \
    -exec jq --null-inputn --compact-outputc \
      '$ARGS.positional[] as $path | { path: $path, type: "directory" }' \
      --args {} +

Using xargs: Use -print0 with find and -0 with xargs to safely pass the found pathnames from find to xargs. The jq expression is identical to the above, only the manner in whichhow the pathnames are passed between find and jq differs.

find "$HOME" -maxdepth 1 -type d -print0 |
xargs -0 jq --null-inputn --compact-outputc \
  '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args

With both approaches above, jq would encode the found pathnames toso that they can be able to represent themrepresented as JSON strings.

An alternative jq expression, with the same effect as

$ARGS.positional[] as $path | { path: $path, type: "directory" }

is

$ARGS.positional | map({ path: ., type: "directory" })[]

To read lines into a set of objects likeas you show, you may use the following jq command, which reads from its standard input stream:

jq -R -c '{ path: ., type: "directory" }'

This answer assumes that your filenames (or whatever text you wish to JSON encode) is valid UTF-8.

Two alternatives:

Not using xargs: Call jq with the pathnames as positional parameters directly from find with -exec. Read the found pathnames with --args and access them as the array $ARGS.positional[] in the jq expression. For each pathname, create a JSON object.

find "$HOME" -maxdepth 1 -type d \
    -exec jq --null-input --compact-output \
      '$ARGS.positional[] as $path | { path: $path, type: "directory" }' \
      --args {} +

Using xargs: Use -print0 with find and -0 with xargs to safely pass the found pathnames from find to xargs. The jq expression is identical to the above, only the manner in which the pathnames are passed between find and jq differs.

find "$HOME" -maxdepth 1 -type d -print0 |
xargs -0 jq --null-input --compact-output \
  '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args

With both approaches above, jq would encode the found pathnames to be able to represent them as JSON strings.


To read lines into a set of objects like you show, you may use the following jq command, which reads from its standard input stream:

jq -R -c '{ path: ., type: "directory" }'

This answer assumes that your filenames (or whatever text you wish to JSON encode) is valid UTF-8.

Two alternatives:

Not using xargs: Call jq with the pathnames as positional parameters directly from find with -exec. Read the found pathnames with --args and access them as the array $ARGS.positional in the jq expression. For each pathname, create a JSON object.

find "$HOME" -maxdepth 1 -type d \
    -exec jq -n -c \
      '$ARGS.positional[] as $path | { path: $path, type: "directory" }' \
      --args {} +

Using xargs: Use -print0 with find and -0 with xargs to safely pass the found pathnames from find to xargs. The jq expression is identical to the above, only how the pathnames are passed between find and jq differs.

find "$HOME" -maxdepth 1 -type d -print0 |
xargs -0 jq -n -c \
  '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args

With both approaches above, jq would encode the found pathnames so that they can be represented as JSON strings.

An alternative jq expression, with the same effect as

$ARGS.positional[] as $path | { path: $path, type: "directory" }

is

$ARGS.positional | map({ path: ., type: "directory" })[]

To read lines into a set of objects as you show, you may use the following jq command, which reads from its standard input stream:

jq -R -c '{ path: ., type: "directory" }'

This answer assumes that your filenames (or whatever text you wish to JSON encode) is valid UTF-8.

Two alternatives:

Not using xargs: Call jq with the pathnames as positional parameters directly from find with -exec. Read the found pathnames with --args and access them as the array $ARGS.positional[] in the jq expression. For each pathname, create a JSON object.

find "$HOME" -maxdepth 1 -type d \
    -exec jq -n-null-input -c-compact-output \
      '$ARGS.positional[] as $path | { path: $path, type: "directory" }' \
      --args {} +

Using xargs: Use -print0 with find and -0 with xargs to safely pass the found pathnames from find to xargs. The jq expression is identical to the above, only the manner in which the pathnames are passed between find and jq differs.

find "$HOME" -maxdepth 1 -type d -print0 |
xargs -0 jq -n-null-input -c-compact-output \
  '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args

With both approaches above, jq would encode the found pathnames to be able to represent them as JSON strings.


To read lines into a set of objects like you show, you may use the following jq command, which reads from its standard input stream:

jq -R -c '{ path: ., type: "directory" }'

This answer assumes that your filenames (or whatever text you wish to JSON encode) is valid UTF-8.

Two alternatives:

Not using xargs: Call jq with the pathnames as positional parameters directly from find with -exec. Read the found pathnames with --args and access them as the array $ARGS.positional[] in the jq expression. For each pathname, create a JSON object.

find "$HOME" -maxdepth 1 -type d \
    -exec jq -n -c '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args {} +

Using xargs: Use -print0 with find and -0 with xargs to safely pass the found pathnames from find to xargs. The jq expression is identical to the above, only the manner in which the pathnames are passed between find and jq differs.

find "$HOME" -maxdepth 1 -type d -print0 |
xargs -0 jq -n -c '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args

With both approaches above, jq would encode the found pathnames to be able to represent them as JSON strings.


To read lines into a set of objects like you show, you may use the following jq command, which reads from its standard input stream:

jq -R -c '{ path: ., type: "directory" }'

This answer assumes that your filenames (or whatever text you wish to JSON encode) is valid UTF-8.

Two alternatives:

Not using xargs: Call jq with the pathnames as positional parameters directly from find with -exec. Read the found pathnames with --args and access them as the array $ARGS.positional[] in the jq expression. For each pathname, create a JSON object.

find "$HOME" -maxdepth 1 -type d \
    -exec jq --null-input --compact-output \
      '$ARGS.positional[] as $path | { path: $path, type: "directory" }' \
      --args {} +

Using xargs: Use -print0 with find and -0 with xargs to safely pass the found pathnames from find to xargs. The jq expression is identical to the above, only the manner in which the pathnames are passed between find and jq differs.

find "$HOME" -maxdepth 1 -type d -print0 |
xargs -0 jq --null-input --compact-output \
  '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args

With both approaches above, jq would encode the found pathnames to be able to represent them as JSON strings.


To read lines into a set of objects like you show, you may use the following jq command, which reads from its standard input stream:

jq -R -c '{ path: ., type: "directory" }'
added 48 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

This answer assumes that your filenames (or whatever text you wish to JSON encode) is valid UTF-8.

Two alternatives:

Not using xargs: Call jq with the pathnames as positional parameters directly from find with -exec. Read thesethe found pathnames with --args and access them as the array $ARGS.positional[] in the jq expression. For each pathname, create a JSON object.

find "$HOME" -maxdepth 1 -type d \
    -exec jq -n -c '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args {} +

Using xargs: Use -print0 with find and -0 with xargs to safely pass the found pathnames from find to xargs. The jq expression is identical to the above, only the manner in which the pathnames are passed between find and jq differs.

find "$HOME" -maxdepth 1 -type d -print0 |
xargs -0 jq -n -c '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args

With both approaches above, jq would encode the found pathnames to be able to represent them as JSON strings.


To read lines into a set of objects like you show, you may use the following jq command, which reads from its standard input stream:

jq -R -c '{ path: ., type: "directory" }'

This answer assumes that your filenames (or whatever text you wish to JSON encode) is valid UTF-8.

Two alternatives:

Not using xargs: Call jq with the pathnames as positional parameters. Read these with --args and access them as the array $ARGS.positional[] in the jq expression. For each pathname, create a JSON object.

find "$HOME" -maxdepth 1 -type d \
    -exec jq -n -c '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args {} +

Using xargs: Use -print0 with find and -0 with xargs to safely pass the found pathnames from find to xargs. The jq expression is identical to the above, only the manner in which the pathnames are passed between find and jq differs.

find "$HOME" -maxdepth 1 -type d -print0 |
xargs -0 jq -n -c '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args

With both approaches above, jq would encode the found pathnames to be able to represent them as JSON strings.


To read lines into a set of objects like you show, you may use the following jq command, which reads from its standard input stream:

jq -R -c '{ path: ., type: "directory" }'

This answer assumes that your filenames (or whatever text you wish to JSON encode) is valid UTF-8.

Two alternatives:

Not using xargs: Call jq with the pathnames as positional parameters directly from find with -exec. Read the found pathnames with --args and access them as the array $ARGS.positional[] in the jq expression. For each pathname, create a JSON object.

find "$HOME" -maxdepth 1 -type d \
    -exec jq -n -c '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args {} +

Using xargs: Use -print0 with find and -0 with xargs to safely pass the found pathnames from find to xargs. The jq expression is identical to the above, only the manner in which the pathnames are passed between find and jq differs.

find "$HOME" -maxdepth 1 -type d -print0 |
xargs -0 jq -n -c '$ARGS.positional[] as $path | { path: $path, type: "directory" }' --args

With both approaches above, jq would encode the found pathnames to be able to represent them as JSON strings.


To read lines into a set of objects like you show, you may use the following jq command, which reads from its standard input stream:

jq -R -c '{ path: ., type: "directory" }'
added 153 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Loading
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Loading