使用JQ创建新对象,其中键来自一个对象,值来自另一个对象

发布于 2025-01-10 23:52:11 字数 2365 浏览 1 评论 0原文

我有以下输入:

{
  "Columns": [
    {
      "email": 123,
      "name": 456,
      "firstName": 789,
      "lastName": 450,
      "admin": 900,
      "licensedSheetCreator": 617,
      "groupAdmin": 354,
      "resourceViewer": 804,
      "id": 730,
      "status": 523,
      "sheetCount": 298
    }
  ]
}
{
  "Users": [
    {
      "email": "[email protected]",
      "name": "Abc Def",
      "firstName": "Abc",
      "lastName": "Def",
      "admin": false,
      "licensedSheetCreator": true,
      "groupAdmin": false,
      "resourceViewer": true,
      "id": 521,
      "status": "ACTIVE",
      "sheetCount": 0
    },
    {
      "email": "[email protected]",
      "name": "Aaa Bob",
      "firstName": "Aaa",
      "lastName": "Bob",
      "admin": false,
      "licensedSheetCreator": true,
      "groupAdmin": false,
      "resourceViewer": false,
      "id": 352,
      "status": "ACTIVE",
      "sheetCount": 0
    }
  ]
}

我需要更改用户中所有键值对的键以匹配列中的值,如下所示:

{
  "Columns": [
    {
      "email": 123,
      "name": 456,
      "firstName": 789,
      "lastName": 450,
      "admin": 900,
      "licensedSheetCreator": 617,
      "groupAdmin": 354,
      "resourceViewer": 804,
      "id": 730,
      "status": 523,
      "sheetCount": 298
    }
  ]
}
{
  "Users": [
    {
      123: "[email protected]",
      456: "Abc Def",
      789: "Abc",
      450: "Def",
      900: false,
      617: true,
      354: false,
      804: true,
      730: 521,
      523: "ACTIVE",
      298: 0
    },
    {
      123: "[email protected]",
      456: "Aaa Bob",
      789: "Aaa",
      450: "Bob",
      900: false,
      617: true,
      354: false,
      804: false,
      730: 352,
      523: "ACTIVE",
      298: 0
    }
  ]
}

我不介意更新用户数组或创建新的对象数组。 我尝试了几种与条目、到条目、从条目的组合,尝试使用变量搜索键,但我越深入它,我就越困惑。

I have the following input:

{
  "Columns": [
    {
      "email": 123,
      "name": 456,
      "firstName": 789,
      "lastName": 450,
      "admin": 900,
      "licensedSheetCreator": 617,
      "groupAdmin": 354,
      "resourceViewer": 804,
      "id": 730,
      "status": 523,
      "sheetCount": 298
    }
  ]
}
{
  "Users": [
    {
      "email": "[email protected]",
      "name": "Abc Def",
      "firstName": "Abc",
      "lastName": "Def",
      "admin": false,
      "licensedSheetCreator": true,
      "groupAdmin": false,
      "resourceViewer": true,
      "id": 521,
      "status": "ACTIVE",
      "sheetCount": 0
    },
    {
      "email": "[email protected]",
      "name": "Aaa Bob",
      "firstName": "Aaa",
      "lastName": "Bob",
      "admin": false,
      "licensedSheetCreator": true,
      "groupAdmin": false,
      "resourceViewer": false,
      "id": 352,
      "status": "ACTIVE",
      "sheetCount": 0
    }
  ]
}

I need to change the key for all key value pairs in users to match the value in Columns, like so:

{
  "Columns": [
    {
      "email": 123,
      "name": 456,
      "firstName": 789,
      "lastName": 450,
      "admin": 900,
      "licensedSheetCreator": 617,
      "groupAdmin": 354,
      "resourceViewer": 804,
      "id": 730,
      "status": 523,
      "sheetCount": 298
    }
  ]
}
{
  "Users": [
    {
      123: "[email protected]",
      456: "Abc Def",
      789: "Abc",
      450: "Def",
      900: false,
      617: true,
      354: false,
      804: true,
      730: 521,
      523: "ACTIVE",
      298: 0
    },
    {
      123: "[email protected]",
      456: "Aaa Bob",
      789: "Aaa",
      450: "Bob",
      900: false,
      617: true,
      354: false,
      804: false,
      730: 352,
      523: "ACTIVE",
      298: 0
    }
  ]
}

I don't mind if I update the Users array or create a new array of objects.
I have tried several combinations of with entries, to entries, from entries, trying to search for keys using variables but the more I dive into it, the more confused I get.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空气里的味道 2025-01-17 23:52:11

流的元素是独立处理的。所以我们必须改变输入。

我们可以将流元素分组到一个数组中。对于输入流,可以使用 --slurp/-s 来实现。[1]

jq -s '
   ( .[0].Columns[0] | map_values( tostring ) ) as $map |
   (
      .[0],
      (
         .[1:][] |
         .Users[] |= with_entries(
            .key = $map[ .key ]
         )
      )
   )
'

jqplay 上的演示

或者,我们可以使用 --null-input/-n与 结合input 和/或 inputs 来读取输入。

jq -n '
   input |
   ( .Columns[0] | map_values( tostring ) ) as $map |
   (
      .,
      (
         inputs |
         .Users[] |= with_entries(
            .key = $map[ .key ]
         )
      )
   )
'

jqplay 上的 演示

请注意,您所需的输出不是有效的 JSON。对象键必须是字符串。因此,上面生成的文档与所要求的略有不同。

请注意,我假设 .Columns 始终是一个仅包含一个元素的数组。这是一个无稽之谈的假设,但这是这个问题有意义的唯一方式。


  1. 对于代码生成的流,您可以将流生成器放置在数组构造函数 ([]) 中。 reduce 也可用于从流中收集。例如,map( ... ) 可以写为 [ .[] | ...] 并将 .[] 简化为 $_ ( []; . + [ $_ | ... ] )。

Elements of a stream are processed independently. So we have to change the input.

We could group the stream elements into an array. For an input stream, this can be achieved using --slurp/-s.[1]

jq -s '
   ( .[0].Columns[0] | map_values( tostring ) ) as $map |
   (
      .[0],
      (
         .[1:][] |
         .Users[] |= with_entries(
            .key = $map[ .key ]
         )
      )
   )
'

Demo on jqplay

Alternatively, we could use --null-input/-n in conjunction with input and/or inputs to read the input.

jq -n '
   input |
   ( .Columns[0] | map_values( tostring ) ) as $map |
   (
      .,
      (
         inputs |
         .Users[] |= with_entries(
            .key = $map[ .key ]
         )
      )
   )
'

Demo on jqplay

Note that your desired output isn't valid JSON. Object keys must be strings. So the above produces a slightly different document than requested.

Note that I assumed that .Columns is always an array of one exactly one element. This is a nonsense assumption, but it's the only way the question makes sense.


  1. For a stream the code generates, you could place the stream generator in an array constructor ([]). reduce can also be used to collect from a stream. For example, map( ... ) can be written as [ .[] | ... ] and as reduce .[] as $_ ( []; . + [ $_ | ... ] ).
童话 2025-01-17 23:52:11

以下内容具有简单的优点,尽管它没有对键进行排序。
它假设使用 -n 选项调用 jq,当然会生成有效的 JSON 对象流:

input
| . as $Columns
| .Columns[0] as $dict
| input # Users
| .Users[] |= with_entries(.key |= ($dict[.]|tostring))
| $Columns, .

如果对键进行排序很重要,那么您可以轻松添加合适的代码来执行此操作;或者,如果您不介意对所有对象的键进行排序,则可以使用 -S 命令行选项。

The following has the merit of simplicity, though it does not sort the keys.
It assumes jq is invoked with the -n option and of course produces a stream of valid JSON objects:

input
| . as $Columns
| .Columns[0] as $dict
| input # Users
| .Users[] |= with_entries(.key |= ($dict[.]|tostring))
| $Columns, .

If having the keys sorted is important, then you could easily add suitable code to do that; alternatively, if you don't mind having the keys of all objects sorted, you could use the -S command-line option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文