英文:
jq question regarding transposing json dataset
问题
Sure, here's the translation of the content you provided:
JSON文件
{
"30814": {
"uid": "Joe.Jones",
"date": "2023-05-02",
"reg": "7.13",
"ot": "0.00"
},
"30870": {
"uid": "Joe.Jones",
"date": "2023-05-03",
"reg": "7.48",
"ot": "0.00"
},
"30906": {
"uid": "Hank.Joans",
"date": "2023-05-02",
"reg": "6.37",
"ot": "0.00"
},
"31079": {
"uid": "Some.User",
"date": "2023-05-01",
"reg": "5.82",
"ot": "0.00"
},
"31125": {
"uid": "Some.User",
"date": "2023-05-02",
"reg": "7.28",
"ot": "0.00"
}
}
期望的输出
<table border=1>
<tr>
<td> NA
<td> 2023-05-01 <td> 2023-05-02 <td> 2023-05-03
<tr>
<td> Joe.Jones
<td> - <td> 7.13/0.00 <td> 7.48/0.00
<tr>
<td> Hank.Jones
<td> - <td> 6.37/0.00 <td> -
<tr>
<td> Some.User
<td> 5.82/0.00 <td> 7.28/0.00 <td> -
</table>
I've provided the translated content, as requested.
英文:
So I have an interesting issue. I have a json file that I need to convert in the following format preferably using Linux CLI tools. Is JQ a vehicle for that? if so I can use help with that. If not, I'd love to know the logic with which to do it. My language of choice is PHP but I can convert pseudocode to actual code in php if you provide logic.
JSON FILE
{
"30814": {
"uid": "Joe.Jones",
"date": "2023-05-02",
"reg": "7.13",
"ot": "0.00"
},
"30870": {
"uid": "Joe.Jones",
"date": "2023-05-03",
"reg": "7.48",
"ot": "0.00"
},
"30906": {
"uid": "Hank.Joans",
"date": "2023-05-02",
"reg": "6.37",
"ot": "0.00"
},
"31079": {
"uid": "Some.User",
"date": "2023-05-01",
"reg": "5.82",
"ot": "0.00"
},
"31125": {
"uid": "Some.User",
"date": "2023-05-02",
"reg": "7.28",
"ot": "0.00"
}
}
EXPECTED OUTPUT
<table border=1>
<tr>
<td> NA
<td> 2023-05-01 <td> 2023-05-02 <td> 2023-05-03
<tr>
<td> Joe.Jones
<td> - <td> 7.13/0.00 <td> 7.48/0.00
<tr>
<td> Hank.Jones
<td> - <td> 6.37/0.00 <td> -
<tr>
<td> Some.User
<td> 5.82/0.00 <td> 7.28/0.00 <td> -
</table>
I have a complete brainfog and don't even know what to try. I tried looking up google but this is a specific enough usecase where google searches are not helping me - or I am not searching for right things. The word transposing datasets keeps coming up in my searches but I don't know how to apply logic to solve this.
答案1
得分: 2
除了数据行的排序之外,这是一个完全使用jq的解决方案:
<table border=1>
<tr><td>NA</td><td>2021-01-01</td><td>2021-01-02</td><td>2021-01-03</td></tr>
<tr><td>1</td><td>-</td><td>5</td><td>-</td></tr>
<tr><td>2</td><td>10</td><td>-</td><td>15</td></tr>
</table>
该想法首先生成每一行的一个JSON数组,然后将结果转换为HTML。
英文:
Except for the ordering of the data rows, here's an all-jq solution:
jq -r '
def tocell:
"<td>\(.)</td>";
def torow:
"<tr>",
(.[]|tocell),
"</tr>";
def totable:
"<table border=1>",
(.[] | torow),
"</table>";
[[.[]]
| (map(.date) | unique) as $dates
| ["NA"] + $dates, # header row
(group_by(.uid)
| .[]
| .[0].uid as $uid
| INDEX(.date) as $dict
| [$uid, $dict[$dates[]].reg]
| map(. // "-") ) ]
| totable
'
The idea is first to produce one JSON array per row, and then to convert the result to HTML.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论