【发布时间】:2021-05-27 09:57:09
【问题描述】:
我在 MongoDB 数据库中有数据,其中一列是 JSON 数组。现在我只想从该 JSON Array 列中检索第一个对象。
输入列:
**[{"id" : "25",
"name" : "spot1" ,
"node" : "start_node",
"order" : "1",
"status" : "completed",
"location" : "gate1"},
{"name" : "spot2",
"order" : "2" },
{ "name" : "spot3",
"node" : "start_node",
"status" : "pending" }]**
必需的输出列:
`status:`
completed
我只需要数组中的第一个值。我尝试在 Talend 中使用 tExtractJSONFields 组件。并将 JSON 循环设置为“$.status”。但我得到的输出为
`status:`
completed
null
pending
编辑后:
[{"id" : "25",
"name" : "spot1" ,
"node" : "start_node",
"order" : "1",
"status" : "completed" ,
"location" : "gate1"},
{"name" : "spot2",
"order" : "2" },
{ "name" : "spot3",
"node" : "start_node",
"status" : "pending" } ]
[{"id" : "26",
"name" : "pull1" ,
"node" : "start_node",
"order" : "3",
"status" : "arrived" ,
"location" : "gate3"},
{"name" : "pull2",
"order" : "4" },
{ "name" : "pull3",
"node" : "end_node",
"status" : "pending" } ]
[{"id" : "27",
"name" : "task1" ,
"node" : "start_node",
"order" : "5",
"status" : "pending" ,
"location" : "gate12"},
{"name" : "task2",
"order" : "6" },
{ "name" : "task3",
"node" : "end_node",
"status" : "pending" } ]
预期输出:
status
completed
arrived
pending
【问题讨论】:
-
查看这篇文章:stackoverflow.com/questions/47590235/jsonpath-return-first-find。似乎您的解决方案在那里(虽然没有投票)
-
@Corentin 我在 tExtractJSOFields 中尝试了 "$[0].status" 表达式,但我只得到空值