有关转置 JSON 数据集的 jq 问题。

huangapple go评论66阅读模式
英文:

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

{
&quot;30814&quot;: {
    &quot;uid&quot;:       &quot;Joe.Jones&quot;,
    &quot;date&quot;:       &quot;2023-05-02&quot;,
    &quot;reg&quot;:       &quot;7.13&quot;,
    &quot;ot&quot;:       &quot;0.00&quot;
},
&quot;30870&quot;: {
    &quot;uid&quot;:       &quot;Joe.Jones&quot;,
    &quot;date&quot;:       &quot;2023-05-03&quot;,
    &quot;reg&quot;:       &quot;7.48&quot;,
    &quot;ot&quot;:       &quot;0.00&quot;
},
&quot;30906&quot;: {
    &quot;uid&quot;:       &quot;Hank.Joans&quot;,
    &quot;date&quot;:       &quot;2023-05-02&quot;,
    &quot;reg&quot;:       &quot;6.37&quot;,
    &quot;ot&quot;:       &quot;0.00&quot;
},
&quot;31079&quot;: {
    &quot;uid&quot;:       &quot;Some.User&quot;,
    &quot;date&quot;:       &quot;2023-05-01&quot;,
    &quot;reg&quot;:       &quot;5.82&quot;,
    &quot;ot&quot;:       &quot;0.00&quot;
},
&quot;31125&quot;: {
    &quot;uid&quot;:       &quot;Some.User&quot;,
    &quot;date&quot;:       &quot;2023-05-02&quot;,
    &quot;reg&quot;:       &quot;7.28&quot;,
    &quot;ot&quot;:       &quot;0.00&quot;
}

}

EXPECTED OUTPUT

&lt;table border=1&gt;
&lt;tr&gt;
 &lt;td&gt; NA
 &lt;td&gt; 2023-05-01 &lt;td&gt; 2023-05-02 &lt;td&gt; 2023-05-03
&lt;tr&gt;
  &lt;td&gt; Joe.Jones
  &lt;td&gt; -  &lt;td&gt; 7.13/0.00  &lt;td&gt; 7.48/0.00
&lt;tr&gt;
  &lt;td&gt; Hank.Jones
  &lt;td&gt; - &lt;td&gt; 6.37/0.00 &lt;td&gt; -
&lt;tr&gt;
  &lt;td&gt; Some.User
  &lt;td&gt; 5.82/0.00 &lt;td&gt; 7.28/0.00 &lt;td&gt; -

&lt;/table&gt;

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 &#39;

def tocell:
  &quot;&lt;td&gt;\(.)&lt;/td&gt;&quot;;

def torow:
  &quot;&lt;tr&gt;&quot;,
  (.[]|tocell),
  &quot;&lt;/tr&gt;&quot;;

def totable: 
  &quot;&lt;table border=1&gt;&quot;,
  (.[] | torow),
  &quot;&lt;/table&gt;&quot;;

[[.[]] 
 | (map(.date) | unique) as $dates
 | [&quot;NA&quot;] + $dates,  # header row
   (group_by(.uid)
    | .[]
    | .[0].uid as $uid
    | INDEX(.date) as $dict
    | [$uid, $dict[$dates[]].reg]
    | map(. // &quot;-&quot;) ) ]
| totable
&#39;

The idea is first to produce one JSON array per row, and then to convert the result to HTML.

huangapple
  • 本文由 发表于 2023年5月30日 09:56:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定