1

I have an array of objects, where each object is structured as:

 {
   "impl": "pmdk",
   "pc_writes": 50,
   "threads": 1,
   "throughput (K tps)": 703014,
   "med_latency (ns)": 1334,
   "99_latency (ns)": 2358,
   "exec_time (s)": 14224471006
 }

I would like to divide the "throughput (K tps)" field in each object by 1000 and return the array in the same format. I tried using map_values in the following way:

map_values(."throughput (K tps)"/1000)

But it only returns an array of modified values as:

[703.014,...]

Instead of the entire objects, like this:

 [{
   "impl": "pmdk",
   "pc_writes": 50,
   "threads": 1,
   "throughput (K tps)": 703.014,
   "med_latency (ns)": 1334,
   "99_latency (ns)": 2358,
   "exec_time (s)": 14224471006
 },
 ...
 ]

How do I return an array of objects after dividing one of its fields by 1000?

3 Answers 3

3

Tell jq to replace the value in "throughput (K tps)" by itself divided by 1000:

jq '.[]."throughput (K tps)" |= . / 1000' file.json
Sign up to request clarification or add additional context in comments.

Comments

3

You can use either of map or map_values for your problem. But the problem with your attempt was you were only modifying the field "throughput (K tps)" for printing into output.

Without the |= update assignment operator, only the field mentioned in the expression will be printed to console.

You can use the Update-assignment operator

map(."throughput (K tps)" |= . / 1000)

But jq gives you much more to do Arithmetic update assignments of form +=, *= and also /=, so that you can do

map(."throughput (K tps)" /= 1000)

Comments

0
jq '.[] | ."throughput (K tps)" |= . / 1000'

Should edit each throughput (K tps) by dividing it by 1000.

Try it online!


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.