【问题标题】:JQ: Cannot index string with string "result"JQ:无法使用字符串“结果”索引字符串
【发布时间】:2021-01-06 11:41:18
【问题描述】:

所以我有很多遵循这种结构的文件。每个包含超过 500 行。

file1.json:

{"result": {"lead": [{"id": "1234", "accountID": null, "ownerID": "5678", "companyName": "Hello", "title": "Manager", "firstName": "Nick", "lastName": "Name", "street": null, "city": "Somewhere", "country": "Canada", "state": null, "zipcode": null, "emailAddress": "someone@somewhere.com", "website": "somewhere.com", "phoneNumber": "", "officePhoneNumber": null, "phoneNumberExtension": null, "mobilePhoneNumber": "", "faxNumber": null, "description": "Website", "campaignID": "12345678", "trackingID": null, "industry": null, "active": "1", "isQualified": "1", "isContact": "1", "isCustomer": "0", "status": "3", "updateTimestamp": "2020-11-06 09:44:04", "createTimestamp": "2018-07-17 15:59:42", "leadScoreWeighted": "6", "leadScore": "15", "isUnsubscribed": "1", "leadStatus": "contactWithOpp", "persona": ""}]}}

file2.json

{"result": {"lead": [{"id": "2234", "accountID": null, "ownerID": "5678", "companyName": "Hello", "title": "Manager", "firstName": "Martin", "lastName": "Name", "street": null, "city": "Somewhere", "country": "Canada", "state": null, "zipcode": null, "emailAddress": "someone@somewhere.com", "website": "somewhere.com", "phoneNumber": "", "officePhoneNumber": null, "phoneNumberExtension": null, "mobilePhoneNumber": "", "faxNumber": null, "description": "Website", "campaignID": "12345678", "trackingID": null, "industry": null, "active": "1", "isQualified": "1", "isContact": "1", "isCustomer": "0", "status": "3", "updateTimestamp": "2020-11-06 09:44:04", "createTimestamp": "2018-07-17 15:59:42", "leadScoreWeighted": "6", "leadScore": "15", "isUnsubscribed": "1", "leadStatus": "contactWithOpp", "persona": ""}]}}

初始JQ:

.result.lead[].id

他们开始:

{
  "result": {
    "lead": [
      {
        "id": "1234", 

每个包含一个未命名的对象,包含结果对象,包含一个名为lead的数组,包含对象。

我想将多个文件合并处理为一个。我可以使用各种结构(例如不同的 json)或格式(例如 CSV)作为输出,但理想情况下我会得到一个输出文件。

JQ 和 JQPlay 说:

jq: error (at <stdin>:0): Cannot index string with string "result"
exit status 5

我确定我的错误很简单,但我看不到它!

谢谢

编辑:

jq -f do-001-flatten-leads.jq  leads1.json

这行得通。

但是要一起处理所有事情(假设我应该啜饮):

jq -f do-001-flatten-leads.jq -s leads1.json leads2.json

显示错误消息“无法使用字符串“结果”索引字符串

编辑:在我之前的示例 (https://jqplay.org/s/Y_UclBq1xa) 中,我在 JQ 中错误地选择了“raw”。我想选择“slurpfiles”,但这不是一个选项。

https://jqplay.org/s/X5XAfDsuV7 中,我添加了两个条目,假设其行为与 slurpfiles 一致。

来自Memory usage of jq's --slurp option,我开始相信我应该避免啜饮,而应该使用多个输入。我没有做任何总结,只是真正选择列和格式,所以某种流式传输似乎更可取。

【问题讨论】:

  • 为什么在 jq play sn-p 中检查了原始输入? (取消选中它应该可以解决您的问题)
  • 你不是在说你想做什么。
  • 谢谢大家。为了清楚起见,我已经更新了问题。对我的描述中的空白表示歉意。

标签: jq


【解决方案1】:

取消选中“原始输入”,您将获得正确的输出。

【讨论】:

  • 谢谢。为了清楚起见,我已经更新了问题。为我的描述中的空白道歉
【解决方案2】:

由于您似乎没有指定要如何组合文件,因此除了提供以下指导之外,很难做更多的事情:

  1. 如你所料,不要使用 -s 选项

  2. 使用 inputs 和 -n 命令行选项,例如如下:

reduce inputs as $in (null; . + $in.result.lead)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多