【问题标题】:JSON array creation in esql在 esql 中创建 JSON 数组
【发布时间】:2022-01-26 00:53:55
【问题描述】:

我是 IIB 的新手,我正在努力在扩展 SQL 中创建以下 JSON 数据。请给我建议。

{
    "name" : "John Doe",
    "age" : -1,
    "known" : false,
    "address" : { "street" : null, "city" : "unknown" },
    "belongings" : ["this", "that", "the other"]
}

我的 ESQL 代码:

DECLARE vm ROW;
    SET vm.Name[] = LIST{13,08,25,06};
    SET OutputRoot.JSON.Data.name = 'John Doe';
    SET OutputRoot.JSON.Data.age  =  1;
    SET OutputRoot.JSON.Data.known = false;
    SET OutputRoot.JSON.Data.address.street = null;
    SET OutputRoot.JSON.Data.address.city = 'Unknown';
    SET OutputRoot.JSON.Data.belongings = vm;

输出:

{
"name":"John Doe",
"age":1,
"known":false,
"address":{"city":"Unknown"},
"belongings":{"Name":13,"Name":8,"Name":25,"Name":6}
}

【问题讨论】:

    标签: json ibm-integration-bus extended-sql


    【解决方案1】:

    这就是创建 JSON 数组的方法:

    CREATE FIELD OutputRoot.JSON.Data.belongings IDENTITY(JSON.Array)belongings;
    SET OutputRoot.JSON.Data.belongings.Item[1]=13;
    SET OutputRoot.JSON.Data.belongings.Item[2]=8;
    SET OutputRoot.JSON.Data.belongings.Item[3]=25;
    SET OutputRoot.JSON.Data.belongings.Item[4]=6;
    

    【讨论】:

      【解决方案2】:

      答案是正确的,但是如果你使用引用来优化代码的性能会更好:-

      DECLARE vm ROW;
      DECLARE refBelong OutputRoot;
      SET vm.Name[] = LIST{13,08,25,06};
      SET OutputRoot.JSON.Data.name = 'John Doe';
      SET OutputRoot.JSON.Data.age  =  1;
      SET OutputRoot.JSON.Data.known = false;
      SET OutputRoot.JSON.Data.address.street = null;
      SET OutputRoot.JSON.Data.address.city = 'Unknown';
      -- Keeping the above code as is
      
      -- Creating a new field for belongings
      CREATE LASTCHILD OF OutputRoot.JSON.Data AS refBelong NAME 'belongings';
      -- This makes the belonging field an Array
      SET refBelong.TYPE = JSON.Array;
      -- The Item field is required to assign the value to JSON Array type
      SET refBelong.Item[1] = 'this';
      SET refBelong.Item[1] = 'that';
      SET refBelong.Item[1] = 'this other';
      

      【讨论】:

        猜你喜欢
        • 2016-09-29
        • 2017-05-30
        • 2015-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        • 2015-04-28
        • 2016-01-25
        相关资源
        最近更新 更多