首页 > 解决方案 > 从 Talend 中的 JSON 文件中提取值

问题描述

我有这样的json文件:

{"2020-04-28": { "37,N7L2H4,Carry,CHOPA,PLX": { "inter_results": { "inter_mark": "GITA" ,"down": null ,"up": null ,"wiki": {"included": "false", "options": ["RRR", "SSS","HHH"] }} ,"38, N5L2J4, HURT, SERRA, PZT": { "inter_results": { "inter_mark": "MARI" ,"down": "250" ,"up": "1250" ,"wiki": {"included": "true", "options": ["XXX", "YYY"] }} ,"39, N4L2H4, HIBA, FILA, PFG": { "inter_results": { "inter_mark": "HILO" ,"down": "100" ,"up": "250" ,"wiki": {"included": "true", "options": ["RTG", "VTH","HJI","JKL"] }} } }

我想使用withN7L2H4,N5L2J4,N4L2H4从此 json 文件中提取值。tFileInputJsonjsonPath

标签: talend

解决方案


使用 Talend 的原生组件,这很难实现。你可以用一些java代码来做,但这并不优雅。
这是使用来自 Talend Exchange 的 json 组件套件的解决方案,您可以在此处下载 在此处输入图像描述

该组件tJSONDocTraverseFields允许您列出 json 的所有字段、路径和值。它给出了这个输出:

$.2020-04-28.37,N7L2H4,Carry,CHOPA,PLX.inter_results.inter_mark|4|inter_mark|"GITA"|false|21
$.2020-04-28.37,N7L2H4,Carry,CHOPA,PLX.inter_results.down|4|down|null|false|21
$.2020-04-28.37,N7L2H4,Carry,CHOPA,PLX.inter_results.up|4|up|null|false|21
$.2020-04-28.37,N7L2H4,Carry,CHOPA,PLX.inter_results.wiki.included|5|included|"false"|false|21
$.2020-04-28.37,N7L2H4,Carry,CHOPA,PLX.inter_results.wiki.options[0]|6|options|"RRR"|true|21
$.2020-04-28.37,N7L2H4,Carry,CHOPA,PLX.inter_results.wiki.options[1]|6|options|"SSS"|true|21
$.2020-04-28.37,N7L2H4,Carry,CHOPA,PLX.inter_results.wiki.options[2]|6|options|"HHH"|true|21
$.2020-04-28.38, N5L2J4, HURT, SERRA, PZT.inter_results.inter_mark|4|inter_mark|"MARI"|false|21
$.2020-04-28.38, N5L2J4, HURT, SERRA, PZT.inter_results.down|4|down|"250"|false|21
$.2020-04-28.38, N5L2J4, HURT, SERRA, PZT.inter_results.up|4|up|"1250"|false|21

然后,您可以解析 json 路径以获取所需的值:
在此处输入图像描述

我用“。”分割路径 获取字段“37,N7L2H4,Carry,CHOPA,PLX”,然后在“”上再次拆分结果并获取第一个值。
tJSONDocOpen允许您初始化 json 文件,它充当连接。然后在 中选择它tJSONDocTraverseFields


推荐阅读