MLR工具将JSON转换为CSV,并从JSON的所有可能标题中填充丢失的字段

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

MLR tool to convert JSON to CSV and pad missing fields from all possible headers in json

问题

If I have three json objects in : test.json

  1. {
  2. "queue": "A",
  3. "field1": 22,
  4. "field2": 2,
  5. "unique_field": 0
  6. }
  7. {
  8. "queue": "B",
  9. "field1": 39,
  10. "field2": 3
  11. }
  12. {
  13. "queue": "C",
  14. "field1": 336,
  15. "field2": 5
  16. }

Running MLR to convert to CSV returns two unique sets based on headers

  1. ./mlr --j2c cat test.json
  2. queue,field1,field2,unique_field
  3. A,22,2,0
  4. queue,field1,field2
  5. B,39,3
  6. C,336,5

What I would like is for MLR to pad any missing fields with null so only 1 header is present

  1. queue,field1,field2,unique_field
  2. A,22,2,0
  3. B,39,3,null
  4. C,336,5,null

I am not sure what to try yet, as need expertise from MLR community

英文:

If I have three json objects in : test.json

  1. {
  2. "queue": "A",
  3. "field1": 22,
  4. "field2": 2,
  5. "unique_field": 0
  6. }
  7. {
  8. "queue": "B",
  9. "field1": 39,
  10. "field2": 3
  11. }
  12. {
  13. "queue": "C",
  14. "field1": 336,
  15. "field2": 5
  16. }

Running MLR to convert to CSV returns two unique sets based on headers

  1. ./mlr --j2c cat test.json
  2. queue,field1,field2,unique_field
  3. A,22,2,0
  4. queue,field1,field2
  5. B,39,3
  6. C,336,5

What I would like is for MLR to pad any missing fields with null so only 1 header is present

  1. queue,field1,field2,unique_field
  2. A,22,2,0
  3. B,39,3,null
  4. C,336,5,null

I am not sure what to try yet, as need expertise from MLR community

答案1

得分: 2

你必须使用 unsparsify 动词:

  1. ./mlr --j2c unsparsify test.json
英文:

You must use the unsparsify verb:

  1. ./mlr --j2c unsparsify test.json

huangapple
  • 本文由 发表于 2023年4月7日 04:09:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953380.html
匿名

发表评论

匿名网友

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

确定