I'm trying to parse a JSON document and print a couple of values on the same line. Is there a way to take the following document:
{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }
And spit 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 somewhere that shows how to do this I would appreciate any pointers.
You can use the cat() function to easily print multiple variables on the same line in R. This function uses the following basic syntax: cat(variable1, variable2, variable3, ...)
Modify print() method to print on the same line The print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.
To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")
Using endl The endl statement can be used to print the multi-line string in a single cout statement. Below is the C++ program to show the same: C++
The easiest way in your example is to use String Interpolation along with the -r
option. e.g.
echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \ jq -r '.fmep| "\(.foo) \(.bar)"'
produces
112 234324
You may also want to consider putting the values in an array and using @tsv e.g.
echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \ jq -r '.fmep | [.foo, .bar] | @tsv'
which produces tab-separated
112 234324
Here is the syntax using joined output (-j
):
jq -j '.fmep | .foo, " ", .bar, "\n"' payload.json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With