简体   繁体   English

使用jq创建.csv文件

[英]Using jq to create .csv file

This topic has been raised before, but the responses don't quite solve my problem which is detailed below. 之前已经提出了这个主题,但是响应并不能完全解决我的问题,下面将对此进行详细介绍。

I have the following JSON file from an API call.... 我有来自API调用的以下JSON文件。...

{
  "status": "ok",
  "meta": {
    "count": 3
  },
  "data": {
    "1030907370": {
      "client_language": "en",
      "last_battle_time": 1548555418,
      "account_id": 1030907370,
      "created_at": 1525288212,
      "updated_at": 1548557165,
      "private": null,
      "global_rating": 3906,
      "clan_id": null,
      "nickname": "Knightly_Gunner",
      "logout_at": 1548557161
    },
    "1020786770": {
      "client_language": "ja",
      "last_battle_time": 1548948444,
      "account_id": 1020786770,
      "created_at": 1465998341,
      "updated_at": 1548948547,
      "private": null,
      "global_rating": 4396,
      "clan_id": null,
      "nickname": "black_black_boss",
      "logout_at": 1548948544
    },
    "1018984431": {
      "client_language": "en",
      "last_battle_time": 1548550089,
      "account_id": 1018984431,
      "created_at": 1456254986,
      "updated_at": 1548550089,
      "private": null,
      "global_rating": 1666,
      "clan_id": null,
      "nickname": "doctordon",
      "logout_at": 1548550016
    }
  }
}

Using the following jq query.... 使用以下jq查询...。

jq '.data | jq'.data | to_entries[] | to_entries [] | [.key, .value.client_language, .value.global_rating]' [.key,.value.client_language,.value.global_rating]”

I get the following output which is exactly the data I need, neatly packaged in arrays.... 我得到以下输出,正是我所需要的数据,整齐地打包在数组中。...

[
  "1030907370",
  "en",
  3906
]
[
  "1020786770",
  "ja",
  4396
]
[
  "1018984431",
  "en",
  1666
]

I need to upload this data to an MS Access database so it therefore needs to be converted to a .csv file so my query now looks like this.... 我需要将此数据上传到MS Access数据库,因此需要将其转换为.csv文件,因此我的查询现在看起来像这样。

jq '.data | jq'.data | to_entries[] | to_entries [] | [.key, .value.client_language, .value.global_rating] | [.key,.value.client_language,.value.global_rating] | @csv' @csv'

and the resulting output is.... 结果输出是...

"\"1030907370\",\"en\",3906"
"\"1020786770\",\"ja\",4396"
"\"1018984431\",\"en\",1666"

but this is NOT a true .csv file and doesn't work for MS Access or Excel. 但这不是真正的.csv文件,因此不适用于MS Access或Excel。 What I need is output that looks like this.... 我需要的是看起来像这样的输出。

"1030907370","en",3906
"1020786770","ja",4396
"1018984431","en",1666

but this is where my problem lies!. 但这就是我的问题所在! I would appreciate some help in achieving the required output from the jq query. 在实现jq查询所需的输出方面,我将提供一些帮助,我们将不胜感激。 Tks! Tks!

Use raw mode (-r) 使用原始模式(-r)

jq -r '.data | to_entries[] | [.key, .value.client_language, .value.global_rating] | @csv'

--raw-output / -r: --raw-output / -r:

With this option, if the filter's result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. 使用此选项,如果过滤器的结果是字符串,则将其直接写入标准输出,而不是将其格式化为带引号的JSON字符串。 This can be useful for making jq filters talk to non-JSON-based systems. 这对于使jq过滤器与基于非JSON的系统进行通信很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM