Add elements within array with same field name in jq - json

Need to add array elements with same field name
Input:
[
{
"all": 1,
"sys": "bus"
},
{
"all": 14,
"sys": "bus"
}
]
I have tried like below:
.[] | (.all +.all)
but got result like
2
28
Expected result: 15 (1 + 14)

First map, then add:
map(.all) | add
C'est tout.

Related

Array of objects to grid/table in Angular

I have data as an array of objects like this:
[{
"x":1,
"y":10,
"val": 13
},
{
"x":1,
"y":20,
"val": 15
},
{
"x":2,
"y":10,
"val": 12
},
{
"x":2,
"y":20,
"val": 16
}
]
I want this data to be displayed in an HTML table like this:
1 2
10 | 13 12
20 | 15 16
How to proceed?
I would parse the data so you have a 2d array. Something like this:
[
[13,12],
[15,16],
]
and then use two nested ng-for loops to render the content:
<div *ngFor="let row of matrix">
<span *ngFor="let item of row">{{item}}</span>
</div>
and place the headers appart.

How to parse nested json to csv using command line

I want to parse a nested json to csv. The data looks similar to this.
{"tables":[{"name":"PrimaryResult","columns":[{"name":"name","type":"string"},{"name":"id","type":"string"},{"name":"custom","type":"dynamic"}]"rows":[["Alpha","1","{\"age\":\"23\",\"number\":\"xyz\"}]]]}
I want csv file as:
name id age number
alpha 1 23 xyz
I tried:
jq -r ".tables | .[] | .columns | map(.name)|#csv" demo.json > demo.csv
jq -r ".tables | .[] | .rows |.[]|#csv" demo.json >> demo.csv
But I am not getting expected result.
Output:
name id custom
alpha 1 {"age":"23","number":"xyz}
Expected:
name id age number
alpha 1 23 xyz
Assuming valid JSON input:
{
"tables": [
{
"name": "PrimaryResult",
"columns": [
{ "name": "name", "type": "string" },
{ "name": "id", "type": "string" },
{ "name": "custom", "type": "dynamic" }
],
"rows": [
"Alpha",
"1",
"{\"age\":\"23\",\"number\":\"xyz\"}"
]
}
]
}
And assuming fixed headers:
jq -r '["name", "id", "age", "number"],
(.tables[].rows | [.[0,1], (.[2] | fromjson | .age, .number)])
| #csv' input.json
Output:
"name","id","age","number"
"Alpha","1","23","xyz"
If any of the assumptions is wrong, you need to clarify your requirements, e.g.
How are column names determined?
What happens if the input contains multiple tables?
As the "dynamic" object always of the same shape? Or can it sometimes contain fewer, more, or different columns?
Assuming that the .rows array is a 2D array of rows and fields, and that a column of type "dynamic" always expects a JSON-encoded object whose fields represent further columns but may or may not always be present in every row.
Then you could go with transposing the headers array and the rows array in order to integratively process each column by their type, especially collecting all keys from the "dynamic" type on the fly, and then transpose it back to get the row-based CSV output.
Input (I have added another row for illustration):
{
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "name",
"type": "string"
},
{
"name": "id",
"type": "string"
},
{
"name": "custom",
"type": "dynamic"
}
],
"rows": [
[
"Alpha",
"1",
"{\"age\":\"23\",\"number\":\"123\"}"
],
[
"Beta",
"2",
"{\"age\":\"45\",\"word\":\"xyz\"}"
]
]
}
]
}
Filter:
jq -r '
.tables[] | [.columns, .rows[]] | transpose | map(
if first.type == "string" then first |= .name
elif first.type == "dynamic" then
.[1:] | map(fromjson)
| (map(keys[]) | unique) as $keys
| [$keys, (.[] | [.[$keys[]]])] | transpose[]
else empty end
)
| transpose[] | #csv
'
Output:
"name","id","age","number","word"
"Alpha","1","23","123",
"Beta","2","45",,"xyz"
Demo

Remove last character from json output using JQ

I have a json that looks like this:
{
"HostedZones": [
{
"ResourceRecordSetCount": 2,
"CallerReference": "test20150527-2",
"Config": {
"Comment": "test2",
"PrivateZone": true
},
"Id": "/hostedzone/Z119WBBTVP5WFX",
"Name": "dev.devx.company.services."
},
{
"ResourceRecordSetCount": 2,
"CallerReference": "test20150527-1",
"Config": {
"Comment": "test",
"PrivateZone": true
},
"Id": "/hostedzone/Z3P5QSUBK4POTI",
"Name": "test.devx.company.services."
}
],
"IsTruncated": false,
"MaxItems": "100"
}
And my goal is to fetch a specific Name (in my case it's the test.devx.company.services), however the Name field contains an extra "." at the end that I'd like to remove from the output.
This is what I have so far:
jq --raw-output '.HostedZones[] | select(.Name | test("test")?) | (.Name[:-1] | sub("."; ""))'
The problem with that it is removing the first character from the output also.
So the output currently is: est.devx.company.services (JQ play snippet)
Not sure what I'm doing wrong :/
To always remove the last character, if it contains "test":
jq '(.HostedZones[].Name | select(contains("test"))) |= .[:-1]'
To remove it only if it is a dot:
jq '(.HostedZones[].Name | select(contains("test"))) |= sub("[.]$"; "")'

Getting first level with JMESPath

I have this JSON document:
{
"1": {
"a": "G1"
},
"2": {
"a": "GM1"
}
}
My expected result should be:
1,G1
2,GM1
With *.a i get
[
"G1",
"GM1"
]
but I am absolutely stuck for the rest.
Sadly there is not much you can do that would be totally matching your use case and that would scale properly.
This is because JMESPath does not have a way to reference its parent, although this has been requested before, to allow you something like
*.[join(',', [keys($), a])]
You can definitely extract a list of keys and values, thanks to the function keys:
#.{keys: keys(#), values: *.a}
That gives
{
"keys": [
"1",
"2"
],
"values": [
"G1",
"GM1"
]
}
But then you just fall under the same case as this other question, because keys will give you a list of keys.
You can also end with a list of lists:
#.[keys(#), *.a]
Will give you:
[
[
"1",
"2"
],
[
"G1",
"GM1"
]
]
And you can even go further and flatten it if needed:
#.[keys(#), *.a] []
Gives:
[
"1",
"2",
"G1",
"GM1"
]
With all this if you do happen to have a list of exactly two items, then a solution would be to use a combination of join and slice:
#.[join(',',[keys(#),*.a][] | [::2]), join(',',[keys(#),*.a][] | [1::2])]
That would give the expected:
[
"1,G1",
"2,GM1"
]
But, sadly, as soon as you have more than two items to consider you would end up with a buggy:
[
"1,3,G1,GM3",
"2,4,GM1,GM4"
]
With a data set of
{
"1": {
"a": "G1"
},
"2": {
"a": "GM1"
},
"3": {
"a": "GM3"
},
"4": {
"a": "GM4"
}
}
And then, of course, the same can be achieved hardcoding indexes:
#.[join(',', [keys(#)[0], *.a | [0]]), join(',', [keys(#)[1], *.a | [1]])]
That also gives the expected:
[
"1,G1",
"2,GM1"
]
But, sadly, this only works if you know in advance the number of rows that are going to be returned to you.
And if you want a single string, given that were you want to feed the data accepts \n as a new line, you can join he whole array again:
#.[join(',', [keys(#)[0], *.a | [0]]), join(',', [keys(#)[1], *.a | [1]])].join(`\n`,#)
Will give:
"1,G1\n2,GM1"
Finally this expression worked 100% for me:
[{key1:keys(#)[0],a:*.a| [0]},{key1:keys(#)[1],a:*.a| [1]}]

filter json when it must fulfill two conditions. One for the key, and one in a nested array for the selected element with jq

given the following json, how the below code should be altered, so as to add a new condition.
return all ids, that have an element that starts with 4, but also have an array that has in it either the
"v" value, or the "9" value.
Input json:
{
"memo": {
"notice": "delivered on 17"
},
"message": "{\"id\":\"1\",\"401\":[[\" 0\",[\"a\",\"UK\"],[\"b\",\"Euy/ O\"],[\"c\",\"20160811\"],[\"g\",\"R2\"]],[\" 1\",[\"a\",\"UK\"],[\"b\",\"LO\"],[\"c\",\"20160811\"]]]}"
}
{
"memo": {
"notice": "delivered on 190"
},
"message": "{\"id\":\"2\",\"424\":[[\" 0\",[\"v\",\"UK\"],[\"9\",\"Euy/ O\"],[\"c\",\"20160811\"],[\"g\",\"R2\"]],[\" 1\",[\"a\",\"UK\"],[\"b\",\"LO\"],[\"c\",\"20160811\"]]]}"
}
{
"memo": {
"notice": "delivered on 734"
},
"message": "{\"id\":\"3\",\"432\":[[\" 0\",[\"a\",\"UK\"],[\"9\",\"Euy/ O\"],[\"c\",\"20160811\"],[\"v\",\"R2\"]],[\" 1\",[\"a\",\"UK\"],[\"b\",\"LO\"],[\"c\",\"20160811\"]]]}"
}
{
"memo": {
"notice": "delivered on 1092"
},
"message": "{\"id\":\"888\",\"532\":[[\" 0\",[\"v\",\"UK\"],[\"b\",\"Euy/ O\"],[\"c\",\"20160811\"],[\"g\",\"R2\"]],[\" 1\",[\"a\",\"UK\"],[\"b\",\"LO\"],[\"c\",\"20160811\"]]]}"
}
snippet that needs to be adjusted:
< input.json jq -r '.content
| fromjson
| select( any(keys_unsorted[]; test("^4")) )
| .["id"]'
Desired output:
2
3
jq version: 1.6, already tried without success to use:
index
the built-in IN statement
We need only the 001 values, of each json object, that has an element which starts with a 4, and that this element (say 401) also has a value of v, or 9 in its nested array, or both v and 9.
First, the relevant key is .message, not .content.
Second, since you are imposing an additional condition, you have only to use and.
Third, the requirements are a bit ambiguous, but the following produces the expected results, and is a solution to one disambiguation of the stated problem:
.message
| fromjson
| select( any(keys_unsorted[]; test("^4"))
and (.[] | arrays | flatten | (index(["v"]) or index(["9"])) ) )
| .["id"]
As you suggest, you could also use IN.