【问题标题】:How to get first object from JSON array in Talend如何从 Talend 中的 JSON 数组中获取第一个对象
【发布时间】: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

【问题讨论】:

标签: arrays json object talend


【解决方案1】:

我认为这不是您发布的有效 json

我把你的 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;

输出将是:

2021 年 5 月 27 日 14:36 开始工作 Test_。 [统计] 连接到 3903 端口上的套接字 [统计] 连接 完成 [统计] 断开连接

作业测试_于 2021 年 5 月 27 日 14:36 结束。 [退出代码 = 0]

【讨论】:

  • 我需要从状态中获取第一个值。我该如何做到这一点?状态 = 完成
  • 刚刚编辑了我的答案!这就是你实现它的方法
  • 非常感谢。这适用于一张唱片。我有多个记录,必须检索所有 JSON 数组中的第一个元素。
  • 使用其他列调整方法并删除第一个元素
  • 我已经编辑了我的问题,请检查。谢谢
猜你喜欢
  • 2013-07-06
  • 1970-01-01
  • 2021-03-23
  • 2022-01-19
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
相关资源
最近更新 更多