Print multiple values ​​on one line

I am trying to parse a JSON document and print a couple of values ​​in one line. Is there any way to accept the following document:

{
  "fmep": {
    "foo": 112,
    "bar": 234324,
    "cat": 21343423
  }
}

And spit it out:

112 234324

I can get the values ​​I want, but they are printed on separate lines:

$ echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | jq '.fmep|.foo,.bar'

112  
234324

If there is an example showing how to do this, I would appreciate any pointers.

+8
source share
3 answers

The easiest way in your example is to use String Interpolation with -r. eg

echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \
jq -r '.fmep| "\(.foo) \(.bar)"'

produces

112 234324

You can also consider putting values ​​in an array and using @tsv, for example:

echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \
jq -r '.fmep | [.foo, .bar] | @tsv'

which tabs

112 234324
+14

(-j):

jq -j '.fmep | .foo, " ", .bar, "\n"' payload.json
0

walk-path unix JSON - jtc, : walk-path.

, , :

bash $ echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \
jtc -w'[fmep][foo]<f>v[-1][bar]' -T'"{f} {}"' -qq
112 234324
bash $ 

-w (-w) [..] / <..> ( , ), :

  • [fmep][foo] - , fmep foo
  • <f>v - JSON f ( v )
  • [-1][bar] - (, JSON ), bar

-T (-T):

  • {f} - f
  • {} - ( )

- JSON - . JSON , -qq .

PS> : jtc shell cli JSON

0

Source: https://habr.com/ru/post/1685355/


All Articles