JQ:将不同的对象合并到一个新的数组中

时间:2019-04-03 08:57:57

标签: json jq

我在Windows环境中使用jq 1.5修改json文件。 嗨,有以下json文件:

{"player_container":{"74016":["None","None","None","None","None","None","None","None"],"74002":["None","None","None","ta81a928e0624295b01499d7035d0308","tdd4e222600d42f9954b0be0448da0b1","None","None","None"],"74007":["None","None","None","None","None","None","None","None"],"74009":["None","None","None","None","None","None","kcb810a423dd4e179027f0feaae9ea60","None"],"74012":["x720ed571876471fab173ba05f71e4b3","None","None","None","None","None","None","None"],"74013":["None","None","None","None","None","None","None","None"],"74014":["vd86094b5a6e448f8c416d6fe75bec7c","None","None","h6ef696cf767464f9c367150fdb29fe4","None","g77b124cd46b41399c3db219ac0a76bd","None","None"],"74015":["None","None","None","None","None","None","None","None"]}}

在对象player_container中,存在不同的对象(对象的“名称”和对象的数量有时会不同)。 我实际上使用以下jq命令将它们加入新数组:

.player_container | .[] | [{ user_id: .[]}]

但是它生成不同的json数组而不是一个。我需要的是这样的:

   [
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  }
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "ta81a928e0624295b01499d7035d0308"
  },
  {
    "user_id": "tdd4e222600d42f9954b0be0448da0b1"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  }
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  }
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "kcb810a423dd4e179027f0feaae9ea60"
  },
  {
    "user_id": "None"
  }
  {
    "user_id": "x720ed571876471fab173ba05f71e4b3"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  }
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  }
  {
    "user_id": "vd86094b5a6e448f8c416d6fe75bec7c"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "h6ef696cf767464f9c367150fdb29fe4"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "g77b124cd46b41399c3db219ac0a76bd"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  }
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  },
  {
    "user_id": "None"
  }
]

有什么提示吗?

BR 蒂莫

1 个答案:

答案 0 :(得分:0)

您只需移动一个字符:

[.player_container | .[] | { user_id: .[]}]

虽然您的版本为.player_container | .[]的每个输出创建了一个单独的数组,但是这会将所有内容收集到一个数组中。