使用JQ创建新对象,其中键来自一个对象,值来自另一个对象
我有以下输入:
{
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
流的元素是独立处理的。所以我们必须改变输入。
我们可以将流元素分组到一个数组中。对于输入流,可以使用
--slurp
/-s
来实现。[1]jqplay 上的演示
或者,我们可以使用
--null-input
/-n
与 结合input
和/或inputs
来读取输入。jqplay 上的 演示
请注意,您所需的输出不是有效的 JSON。对象键必须是字符串。因此,上面生成的文档与所要求的略有不同。
请注意,我假设
.Columns
始终是一个仅包含一个元素的数组。这是一个无稽之谈的假设,但这是这个问题有意义的唯一方式。[]
) 中。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]Demo on jqplay
Alternatively, we could use
--null-input
/-n
in conjunction withinput
and/orinputs
to read the input.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.[]
).reduce
can also be used to collect from a stream. For example,map( ... )
can be written as[ .[] | ... ]
and asreduce .[] as $_ ( []; . + [ $_ | ... ] )
.以下内容具有简单的优点,尽管它没有对键进行排序。
它假设使用 -n 选项调用 jq,当然会生成有效的 JSON 对象流:
如果对键进行排序很重要,那么您可以轻松添加合适的代码来执行此操作;或者,如果您不介意对所有对象的键进行排序,则可以使用 -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:
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.