Reputation: 544
I have a JSON object as follows
{"Sample" : [{ "key": "KeyName", "values": [ [1025409600000, 10], [1028088000000, -6.3382185140371] ] }]}
Using javascript I can modify any values as follows
data.Sample[0].values.push([1028088000000,0]);
How to perform the similar operation using jq commandline json processor? So the JSON object becomes
{"Sample" : [{ "key": "KeyName", "values": [ [1025409600000, 10], [1028088000000, 0] ] }]}
Thank you.
Upvotes: 0
Views: 115
Reputation: 116750
As it stands there is a bug in the question, as Javascript's Array.push appends to an array.
The jq equivalent of the given expression:
data.Sample[0].values.push([1028088000000,0]);
would be:
.Sample[0].values += [[1028088000000,0]]
or if you want to use the Javascript syntax, you could define def push(x): .[length] = x;
If you want to replace the last value in the values
array by another value, say $x, you could (using jq 1.5 or later) write:
.Sample[0].values[-1] = $x
A more robust (with respect to different jq releases) approach would be:
.Sample[0].values |= (.[length-1] = $x)
With jq 1.5 or later, if you wanted only to change the negative number to 0, you would write:
.Sample[0].values[-1][-1] = 0
Etc.
Upvotes: 2