Talend上的JSON反序列化 [英] JSON Deserialization on Talend

查看:241
本文介绍了Talend上的JSON反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图弄清楚如何在talend组件中反序列化这种json:

            {
          "ryan@toofr.com": {
            "confidence":119,"email":"ryan@toofr.com","default":20
          },
          "rbuckley@toofr.com": {
            "confidence":20,"email":"rbuckley@toofr.com","default":15
          },
          "ryan.buckley@toofr.com": {
            "confidence":18,"email":"ryan.buckley@toofr.com","default":16
          },
          "ryanbuckley@toofr.com": {
            "confidence":17,"email":"ryanbuckley@toofr.com","default":17
          },
          "ryan_buckley@toofr.com": {
            "confidence":16,"email":"ryan_buckley@toofr.com","default":18
          },
          "ryan-buckley@toofr.com": {
            "confidence":15,"email":"ryan-buckley@toofr.com","default":19
          },
          "ryanb@toofr.com": {
            "confidence":14,"email":"ryanb@toofr.com","default":14
          },
          "buckley@toofr.com": {
            "confidence":13,"email":"buckley@toofr.com","default":13
          }
        }

此JSON来自Toofr API,可在此处中找到文档.

实际情况:

对于数据库中检索到的每一行,我都调用API并得到了它(名字,姓氏和公司每次都更改.

有人知道如何修改tExtractJSONField(或使用其他东西)以在tLogRow中显示结果(针对数据库中的每一行)吗?

提前谢谢!

这是我的tExtractJSONfields

解决方案

将tExtractJSONFields与XPath一起使用时,您需要

1)有效的XPath循环点

2)相对于循环路径的有效XPath映射到您的结构

此外,当将XPath与Talend一起使用时,每个值都需要一个键.如果要在其上循环,则密钥无法更改.表示这是无效的:

      {
      "ryan@toofr.com": {
        "confidence":119,"email":"ryan@toofr.com","default":20
      },
      "rbuckley@toofr.com": {
        "confidence":20,"email":"rbuckley@toofr.com","default":15
      },

但是此结构将是有效的:

      {
      "contact": {
        "confidence":119,"email":"ryan@toofr.com","default":20
      },
      "contact": {
        "confidence":20,"email":"rbuckley@toofr.com","default":15
      },

因此,使用正确的数据,循环点可能是/contact.

然后,用于Confidence的映射将为confidence(JSON的名称),对于Email的映射将为email,对于default则相反.

编辑

JSONPath有一些缺点,其中之一是您不能在层次结构中往上走.您可以尝试使用jsonpath.com找出正确的查询

循环表达式可以为$.*.不过,我不确定这是否能满足您的需求-一段时间以来,我一直在Talend中使用JSONPath,这已经有一段时间了.

Trying to figuring out how to deserialize this kind of json in talend components :

            {
          "ryan@toofr.com": {
            "confidence":119,"email":"ryan@toofr.com","default":20
          },
          "rbuckley@toofr.com": {
            "confidence":20,"email":"rbuckley@toofr.com","default":15
          },
          "ryan.buckley@toofr.com": {
            "confidence":18,"email":"ryan.buckley@toofr.com","default":16
          },
          "ryanbuckley@toofr.com": {
            "confidence":17,"email":"ryanbuckley@toofr.com","default":17
          },
          "ryan_buckley@toofr.com": {
            "confidence":16,"email":"ryan_buckley@toofr.com","default":18
          },
          "ryan-buckley@toofr.com": {
            "confidence":15,"email":"ryan-buckley@toofr.com","default":19
          },
          "ryanb@toofr.com": {
            "confidence":14,"email":"ryanb@toofr.com","default":14
          },
          "buckley@toofr.com": {
            "confidence":13,"email":"buckley@toofr.com","default":13
          }
        }

This JSON comes from the Toofr API where documentation can be found here .

Here the actual sitation :

For each line retreived in the database, I call the API and I got this (the first name, the last name and the company change everytime.

Does anyone know how to modify the tExtractJSONField (or use smthing else) to show the results in tLogRow (for each line in the database) ?

Thank you in advance !

EDIT 1:

Here's my tExtractJSONfields :

解决方案

When using tExtractJSONFields with XPath, you need

1) a valid XPath loop point

2) valid XPath mapping to your structure relative to the loop path

Also, when using XPath with Talend, every value needs a key. The key cannot change if you want to loop over it. Meaning this is invalid:

      {
      "ryan@toofr.com": {
        "confidence":119,"email":"ryan@toofr.com","default":20
      },
      "rbuckley@toofr.com": {
        "confidence":20,"email":"rbuckley@toofr.com","default":15
      },

but this structure would be valid:

      {
      "contact": {
        "confidence":119,"email":"ryan@toofr.com","default":20
      },
      "contact": {
        "confidence":20,"email":"rbuckley@toofr.com","default":15
      },

So with the correct data the loop point might be /contact.

Then the mapping for Confidence would be confidence (the name from the JSON), the mapping for Email would be email and vice versa for default.

EDIT

JSONPath has a few disadvantages, one of them being you cannot go higher up in the hierarchy. You can try finding out the correct query with jsonpath.com

The loop expression could be $.*. I am not sure if that will satisfy your need, though - it has been a while since I've been using JSONPath in Talend because of the downsides.

这篇关于Talend上的JSON反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆