【问题标题】:Skip creating JSON_OBJECT if there is no data如果没有数据则跳过创建 JSON_OBJECT
【发布时间】:2020-08-04 23:43:30
【问题描述】:

我正在创建一个具有多个层的 JSON_OBJECT,如果没有匹配的数据,则不应创建 JSON_OBJECT。在示例中,我将展示如何在没有数据的情况下通过跳过一个级别来创建它,但我希望有一种更好、更简单的方法来做到这一点,因为我无法通过这种方式来处理当前的问题。此 JSON 的结束位置无法处理 null JSON_OBJECTS,因此他们希望将它们从文件中排除。

这是一些示例数据: 健康)状况:

行动:

如您所见,其中一个条件没有匹配的操作。 这就是我当前创建 JSON 的方式,它不会根据需要删除额外的 JSON_OBJECT,但确实显示了我如何在需要外语时创建一个级别。

     With      
                                           
-- Create All of the (actions)         
 fAction1 as (                                 
  Select CONCCD, CONDID,     
      Case when PADSecLangCode = '' then   
    json_object(                                 
     'actionID'            : trim(ACTID),      
     'actionDescription' : trim(DESC),    
    'active'                 : ACTIVE)
  Else                                                    
  json_object(              
       'actionID'          : trim(ACTID),      
     'actionDescription' : trim(DESC),    
     'active'                : ACTIVE,     
      'recordTexts' : json_object( 
       'recordText' : json_array( json_object( 
         'languageID'        : trim(PADSecLangCode), 
         'actionDescription' : trim(DESCF),    
         'active'                : ACTIVE )))     )     
  End          
               as sAction1                               
   From PADWCA) 


-- Create All of the Conditions (conditions/actions)         
 , fCondition1 as (                                 
  Select CONCCD,   
   Case when PADSecLangCode = '' then   
    json_object(                                 
     'conditionID'            : trim(CONDID),      
     'conditionDescription' : trim(DESC),    
    'active'                     : ACTIVE,
        'actions' : json_object(
          'action' : json_array( 
         (Select sAction1  
          From fAction1 a
          Where a.CONCCD = c.CONCCD and a.CONDID = c.CONDID
                            ) format json    
                              ) format json  
                                ) format json )       
  Else                                                    
  json_object(    
     'conditionID'            : trim(CONDID),      
     'conditionDescription' : trim(DESC),    
    'active'                     : ACTIVE,
    'recordTexts' : json_object( 
     'recordText' : json_array( json_object( 
       'languageID'             : trim(PADSecLangCode), 
       'conditionDescription' : trim(DESCF),    
          'active'                 : ACTIVE ))),     
        'actions' : json_object(
          'action' : json_array( 
         (Select sAction1  
          From fAction1 a
          Where a.CONCCD = c.CONCCD and a.CONDID = c.CONDID
                        ) format json    
                          ) format json  
                            ) format json   )
End
               as sCondition1  
   From PADWCC  c)  

-- 在条件/动作周围创建一个包装器 , fCondition2 为 (选择 json_object( '条件': json_arrayagg( sCondition1 格式 json)) 作为 sCondition2 来自 fCondition1)

-- 创建最终结果
选择 json_object( '条件' : sCondition2 格式 json) 来自 fCondition2;

这是我目前得到的结果:

{
"conditions": {
    "condition": [
        {
            "conditionID": "038-00068-C32",
            "conditionDescription": "Exclusion service repairs were made to the following areas:",
            "active": "false",
            "recordTexts": {
                "recordText": [
                    {
                        "languageID": "FR",
                        "conditionDescription": "Service de réparations d'exclusion ont été faites aux domaines suivants:",
                        "active": "false"
                    }
                ]
            },
            "actions": {
                "action": []
            }
        },
        {
            "conditionID": "020-00050-C26",
            "conditionDescription": "The area was very clean and in excellent condition!",
            "active": "false",
            "recordTexts": {
                "recordText": [
                    {
                        "languageID": "FR",
                        "conditionDescription": "La zone était très propre et en excellent état!",
                        "active": "false"
                    }
                ]
            },
            "actions": {
                "action": [
                    {
                        "actionID": "001-051-C26",
                        "actionDescription": "Please thank everyone who cleaned the area!",
                        "active": "false",
                        "recordTexts": {
                            "recordText": [
                                {
                                    "languageID": "FR",
                                    "actionDescription": "'il vous plaît remercier tous ceux qui nettoyé la zone!",
                                    "active": "false"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

}

这是我想要得到的结果:

{
"conditions": {
    "condition": [
        {
            "conditionID": "038-00068-C32",
            "conditionDescription": "Exclusion service repairs were made to the following areas:",
            "active": "false",
            "recordTexts": {
                "recordText": [
                    {
                        "languageID": "FR",
                        "conditionDescription": "Service de réparations d'exclusion ont été faites aux domaines suivants:",
                        "active": "false"
                    }
                ]
            },
        },
        {
            "conditionID": "020-00050-C26",
            "conditionDescription": "The area was very clean and in excellent condition!",
            "active": "false",
            "recordTexts": {
                "recordText": [
                    {
                        "languageID": "FR",
                        "conditionDescription": "La zone était très propre et en excellent état!",
                        "active": "false"
                    }
                ]
            },
            "actions": {
                "action": [
                    {
                        "actionID": "001-051-C26",
                        "actionDescription": "Please thank everyone who cleaned the area!",
                        "active": "false",
                        "recordTexts": {
                            "recordText": [
                                {
                                    "languageID": "FR",
                                    "actionDescription": "'il vous plaît remercier tous ceux qui nettoyé la zone!",
                                    "active": "false"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

}

【问题讨论】:

    标签: json db2 db2-400 rpgle


    【解决方案1】:

    编辑:我更新了答案以满足您的要求。这让我回到你原来的问题:我用nullif(xxx, json_object())

    所以

    'actions' : actionsArray format json
    

    现在是

    'actions' : nullif(json_object( /* null if value equals empty object */
                        'action' : actionsArray format json
                        absent on null
                        ), json_object()) format json
    

    关于您的第二条评论,我使用了值,因为它在没有数据时返回“{}”,但您可以肯定地使用 select into 你可以使用

    values ... into :pgmvar
    -- or 
    select ... into :pgmvar from ...
    

    抱歉,您的代码不太容易阅读。我重写了它,因为我认为我已经完成了。 结果的结构有点改变,因为我不明白为什么 recordTexts 对象中的 recordText 数组,对于 action/actions 相同:

    with
    -- Test data, conditions
    PADWCC (CONCCD, CONDID, DESC, DESCF, ACTIVE) as (
        values
        (1, '038-00068-C32','Exclusion service repairs were made to the following areas:', 'Service de réparations d''exclusion ont été faites aux domaines suivants:', 'false'),
        (1, '020-00050-C26','The area was very clean and in excellent condition!', 'La zone était très propre et en excellent état!', 'false')
    ),
    -- Test data, actions
    PADWCA (CONCCD, CONDID, ACTID, DESC, DESCF, ACTIVE) as (
        values
        (1 ,'020-00050-C26', '001-051-C26','Please thank everyone who cleaned the area!', 'S''il vous plaît remercier tous ceux qui nettoyé la zone!', 'false')
        --,(1 ,'020-00050-C26', 'otheraction','otheraction', 'autre action', 'false')
    ),
    -- Step 1: create an array of translations for each action (with only one translation inside)
    actionsTranslations as (
        select
            CONCCD,
            CONDID,
            ACTID,
            json_arrayagg(
                json_object(
                    'languageID' : languageID,
                    'actionDescription' : actionDescription,
                    'active' : active
                    )
                ) 
            as recordTexts
        from
            PADWCA
            cross join lateral ( -- Construct a list of text(s) from PADWCA.DESCF
                values
                ('FR', DESCF)
            ) as texts(languageID, actionDescription)
        where languageID = PADSecLangCode
        group by CONCCD, CONDID, ACTID
    ),
    -- Step 2: Create an object for each action
    actionObjects as (
        select
            CONCCD,
            CONDID,
            ACTID,
            json_object(
                    'actionID' : trim(PADWCA.ACTID),
                    'actionDescription' : trim(DESC),
                    'active' : active,
                    'recordTexts' : nullif(json_object(
                        'recordText' : recordTexts format json absent on null
                        ), json_object()) format json
                    absent on null -- no recordTexts key if null
                ) actionObject
        from
            PADWCA
            left join actionsTranslations using(conccd, condid, actid)
    ),
    -- Step 3: Create an array of actions for each condition
    actions as (
        select CONCCD, CONDID, JSON_ARRAYAGG(actionObject format json) as actionsArray from actionObjects group by CONCCD, CONDID
    ),
    -- Step 4: create an array of translations for each condition (with only one translation inside)
    conditionsTranslations as (
        select
            CONCCD,
            CONDID,
            json_arrayagg(
                json_object(
                    'languageID' : languageID,
                    'conditionDescription' : conditionDescription,
                    'active' : active
                    )
                ) 
            as recordTexts
        from
            PADWCC
            cross join lateral (
                values
                ('FR', DESCF)
            ) as texts(languageID, conditionDescription)
        where languageID = PADSecLangCode
        group by CONCCD, CONDID
    ),
    -- Step 5: Create an object for each condition
    conditionObjects as (
        select
            ca.CONCCD,
            ca.CONDID,
            json_object(
                    'conditionID' : trim(ca.CONDID),
                    'conditionDescription' : trim(DESC),
                    'active' : active,
                    'recordTexts' : nullif(json_object(
                        'recordText' : recordTexts format json absent on null
                        ), json_object()) format json,
                    'actions' : nullif(json_object(
                        'action' : actionsArray format json
                        absent on null
                        ), json_object()) format json
                    absent on null -- absent on null applies to every key of the object so recordTexts AND actions
                ) conditionObject
        from
            PADWCC ca
            left join conditionsTranslations ct on (ct.conccd, ct.condid) = (ca.conccd, ca.condid)
            left join actions ac on (ac.conccd, ac.condid) = (ca.conccd, ca.condid)
    ),
    -- Step 6: Create an array of conditions for each CONCCD
    conditions as (
        select CONCCD, JSON_ARRAYAGG(conditionObject format json) as array from conditionObjects group by CONCCD
    )
    -- Wrap output in an object
    values json_object('conditions' : nullif(json_object('condition' : (select conditions.array from conditions where conccd = 1) format json absent on null), json_object()) format json absent on null);
    --or
    --select json_object('conditions' : nullif(json_object('condition' : conditions.array format json absent on null), json_object()) format json absent on null) from conditions where conccd = 2;
    

    给予

    {
        "conditions": {
            "condition": [
                {
                    "conditionID": "038-00068-C32",
                    "conditionDescription": "Exclusion service repairs were made to the following areas:",
                    "active": "false",
                    "recordTexts": {
                        "recordText": [
                            {
                                "languageID": "FR",
                                "conditionDescription": "Service de réparations d'exclusion ont été faites aux domaines suivants:",
                                "active": "false"
                            }
                        ]
                    }
                },
                {
                    "conditionID": "020-00050-C26",
                    "conditionDescription": "The area was very clean and in excellent condition!",
                    "active": "false",
                    "recordTexts": {
                        "recordText": [
                            {
                                "languageID": "FR",
                                "conditionDescription": "La zone était très propre et en excellent état!",
                                "active": "false"
                            }
                        ]
                    },
                    "actions": {
                        "action": [
                            {
                                "actionID": "001-051-C26",
                                "actionDescription": "Please thank everyone who cleaned the area!",
                                "active": "false",
                                "recordTexts": {
                                    "recordText": [
                                        {
                                            "languageID": "FR",
                                            "actionDescription": "S'il vous plaît remercier tous ceux qui nettoyé la zone!",
                                            "active": "false"
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
    

    【讨论】:

    • 感谢您的回复。在使用真实数据进行测试时,这确实按预期工作。我还有很多要补充的内容,我现在正在处理这个声明。但是我有两件事要添加到其中,但没有找到正确的方法。
    • 1) 我了解操作/操作、条件/条件和 rectordTexts/recordText 通常不需要,但这是我创建 JSON 数据所需的方式。我正在尝试找到一种方法来将此额外级别添加到您的示例中。
    • 2) 在用于创建 json 对象的 SQLRPGLE 程序中,我目前使用它在 IFS 中创建对象,我不确定如何将其修改为您创建的方式json object: -- 创建最终的 json 并放入 IFS Select json_object('header' : json_object( 'instanceName' : trim(:xhdr_Cntry)), 'conditionCategoryData' : sConditionCategoryFull2 format json) INTO :OutFile From fConditionCategoryFull2;
    • @JohnBaeten 我编辑了答案并在开头发表了评论
    猜你喜欢
    • 1970-01-01
    • 2021-12-31
    • 2012-07-27
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多