如何从 Talend 中的 JSON 数组中获取第一个对象
How to get first object from JSON array in Talend
我在 MongoDB 数据库中有数据,其中一列是 JSON 数组。现在我只想从 JSON 数组列中检索第一个对象。
Input_Column :
**[{"id" : "25",
"name" : "spot1" ,
"node" : "start_node",
"order" : "1",
"status" : "completed",
"location" : "gate1"},
{"name" : "spot2",
"order" : "2" },
{ "name" : "spot3",
"node" : "start_node",
"status" : "pending" }]**
必需 output_column :
`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
我认为这不是有效的 json 你有 post
我把你的json改成了这样
你可以在这里测试它:https://jsonformatter.curiousconcept.com/#
[{"id" : "25",
"name" : "spot1" ,
"node" : "start_node",
"order" : "1",
"status" : "completed" ,
"location" : "gate1"},
{"name" : "spot2",
"order" : "2" },
{ "name" : "spot3",
"node" : "start_node",
"status" : "pending" } ]
第一次尝试把你的json放在一个文件里
-> 然后在 元数据
--> 拖放文件 json(元数据)
在你的工作中就把这个结构
javaFlex 是这样配置的
- 在高级设置中不要忘记输入:
导入java.util.ArrayList;
输出将是:
开始工作 Test_ 在 14:36 27/05/2021。
[统计] 连接到端口 3903 上的套接字
[统计]已连接
完成
[统计]断开连接
工作测试_在 14:36 27/05/2021 结束。 [退出代码 = 0]
我在 MongoDB 数据库中有数据,其中一列是 JSON 数组。现在我只想从 JSON 数组列中检索第一个对象。
Input_Column :
**[{"id" : "25",
"name" : "spot1" ,
"node" : "start_node",
"order" : "1",
"status" : "completed",
"location" : "gate1"},
{"name" : "spot2",
"order" : "2" },
{ "name" : "spot3",
"node" : "start_node",
"status" : "pending" }]**
必需 output_column :
`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
我认为这不是有效的 json 你有 post
我把你的json改成了这样
你可以在这里测试它:https://jsonformatter.curiousconcept.com/#
[{"id" : "25",
"name" : "spot1" ,
"node" : "start_node",
"order" : "1",
"status" : "completed" ,
"location" : "gate1"},
{"name" : "spot2",
"order" : "2" },
{ "name" : "spot3",
"node" : "start_node",
"status" : "pending" } ]
第一次尝试把你的json放在一个文件里
-> 然后在 元数据
--> 拖放文件 json(元数据)
在你的工作中就把这个结构
javaFlex 是这样配置的
- 在高级设置中不要忘记输入:
导入java.util.ArrayList;
输出将是:
开始工作 Test_ 在 14:36 27/05/2021。 [统计] 连接到端口 3903 上的套接字 [统计]已连接 完成 [统计]断开连接
工作测试_在 14:36 27/05/2021 结束。 [退出代码 = 0]