I Have the following nested json file which I want to parse with jq tool and print in table form like I show at the end
The input.json structure is like this:
{
"document":{
"page":[
{
"@index":"0",
"image":{
"@data":"ABC",
"@format":"png",
"@height":"620.00",
"@type":"base64encoded",
"@width":"450.00",
"@x":"85.00",
"@y":"85.00"
}
},
{
"@index":"1",
"row":[
{
"column":[
{
"text":""
},
{
"text":{
"#text":"Text1",
"@fontName":"Arial",
"@fontSize":"12.0",
"@height":"12.00",
"@width":"71.04",
"@x":"121.10",
"@y":"83.42"
}
}
]
},
{
"column":[
{
"text":""
},
{
"text":{
"#text":"Text2",
"@fontName":"Arial",
"@fontSize":"12.0",
"@height":"12.00",
"@width":"101.07",
"@x":"121.10",
"@y":"124.82"
}
}
]
}
]
},
{
"@index":"2",
"row":[
{
"column":{
"text":{
"#text":"Text3",
"@fontName":"Arial",
"@fontSize":"12.0",
"@height":"12.00",
"@width":"363.44",
"@x":"85.10",
"@y":"69.62"
}
}
},
{
"column":{
"text":{
"#text":"Text4",
"@fontName":"Arial",
"@fontSize":"12.0",
"@height":"12.00",
"@width":"382.36",
"@x":"85.10",
"@y":"83.42"
}
}
},
{
"column":{
"text":{
"#text":"Text5",
"@fontName":"Arial",
"@fontSize":"12.0",
"@height":"12.00",
"@width":"435.05",
"@x":"85.10",
"@y":"97.22"
}
}
}
]
},
{
"@index":"3"
}
]
}
}
Following the answers of the following question (Parsing nested json with jq) I've tried this code but doesn't work
$ cat file.json | jq .document.page[].row | ["#text", "@x", "@y"] | @csv
The output I'm trying to get is:
#text @x @y
Text1 121.10 83.42
Text2 121.10 124.82
Text3 65.10 69.62
Text4 85.10 83.42
Text5 85.10 97.22
How can achieve this?
Thanks
UPDATE
Thanks so much for the help. I've tried with a real file a bit longer.
I was able to adapt the first peak's solution like below:
["#text", "@data", "@fontName", "@fontSize", "@format", "@height", "@type", "@width", "@x", "@y"],
( ..
| objects
| select(has("#text","@data"))
| [.["#text", "@data", "@fontName", "@fontSize", "@format", "@height", "@type", "@width", "@x", "@y"]]
)
| @tsv
and with new input I get this table:
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| #text | @data | @fontName | @fontSize | @format | @height | @type | @width | @x | @y |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| | ABC | | | png | 620 | base64encoded | 450 | 85 | 85 |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| Text ä 1 | | Tahoma | 12 | | 12 | | 427.79 | 85.1 | 69.62 |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| Text ¢76 | | Tahoma | 12 | | 12 | | 270.5 | 85.1 | 690.72 |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| Text % 5 | | Tahoma | 12 | | 12 | | 130.84 | 358.86 | 690.72 |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| Text 7Ç8 | | Tahoma | 12 | | 12 | | 115.95 | 85.1 | 704.52 |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| Text • 2 Wñ79 | | Tahoma | 8 | | 8.04 | | 398.16 | 121.1 | 68.06 |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| Text | | Tahoma | 12 | | 12 | | 101.5 | 85.1 | 83.42 |
| » 1 A\\\\CÓ | | | | | | | | | |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| Text 12 | | Tahoma | 12 | | 12 | | 312.26 | 189.83 | 83.42 |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| Text 82 | | Tahoma | 12 | | 12 | | 44.99 | 85.1 | 97.22 |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| Text 31 | | Tahoma | 8 | | 8.04 | | 381.83 | 133.1 | 95.66 |
+---------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
If possible, how to add the follwing 3 columns (counter, page and row) to know the corresponding page and row for each line?
The expected output would be like this:
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| counter | page | row | #text | @data | @fontName | @fontSize | @format | @height | @type | @width | @x | @y |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| 1 | 0 | | | ABC | | | png | 620 | base64encoded | 450 | 85 | 85 |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| 2 | 1 | 0 | Text ä 1 | | Tahoma | 12 | | 12 | | 427.79 | 85.1 | 69.62 |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| 3 | 1 | 1 | Text ¢76 | | Tahoma | 12 | | 12 | | 270.5 | 85.1 | 690.72 |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| 4 | 1 | 1 | Text % 5 | | Tahoma | 12 | | 12 | | 130.84 | 358.86 | 690.72 |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| 5 | 2 | 2 | Text 7Ç8 | | Tahoma | 12 | | 12 | | 115.95 | 85.1 | 704.52 |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| 6 | 2 | 0 | Text • 2 Wñ79 | | Tahoma | 8 | | 8.04 | | 398.16 | 121.1 | 68.06 |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| 7 | 2 | 1 | Text » 1 A\\\\CÓ | | Tahoma | 12 | | 12 | | 101.5 | 85.1 | 83.42 |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| 8 | 2 | 1 | Text 12 | | Tahoma | 12 | | 12 | | 312.26 | 189.83 | 83.42 |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| 9 | 2 | 2 | Text 82 | | Tahoma | 12 | | 12 | | 44.99 | 85.1 | 97.22 |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
| 10 | 2 | 2 | Text 31 | | Tahoma | 8 | | 8.04 | | 381.83 | 133.1 | 95.66 |
+-------+------+-----+-------------------+-------+-----------+-----------+---------+---------+---------------+--------+--------+--------+
This is new more representative input file input2.json.
And seeing the Json structure in image below gives and idea about page
number and row
number present in json file and values within them.