Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing multiple values on the same line

Tags:

json

jq

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.

like image 757
Mickster Avatar asked Sep 09 '17 14:09

Mickster


People also ask

How do I print multiple variables on the same line?

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, ...)

How do I print all values on one line?

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.

How do you print multiple items on the same line in Python?

To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")

How do I print multiple items on one line C++?

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++


2 Answers

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 
like image 53
jq170727 Avatar answered Sep 24 '22 05:09

jq170727


Here is the syntax using joined output (-j):

jq -j '.fmep | .foo, " ", .bar, "\n"' payload.json 
like image 32
kenorb Avatar answered Sep 23 '22 05:09

kenorb