从java中的Json字符串生成聚合的Json数据

Posted

技术标签:

【中文标题】从java中的Json字符串生成聚合的Json数据【英文标题】:Generate aggregated Json data from Json string in java 【发布时间】:2020-10-23 09:21:31 【问题描述】:

我已经从 db2 获取了下面的 JSON 字符串,需要对数据进行分组。

SELECT JSON_ARRAY(JSON_OBJECT('id' VALUE id,
                   'items' VALUE JSON_OBJECT('item' value item, 
                                                          'itemScore' value itemScore,
                                                          'stage' value stage,
                                                          'reco' VALUE JSON_OBJECT('product' value product,
                                                                         'url' value url,
                                                                         'score' value score
                                                                         format json)format json absent on null)
                   format json absent on null)format json absent on null)
FROM Product
GROUP BY id, item, itemScore, stage, url, product, score

如果有 Java 或 DB2 查询的解决方案会有所帮助。

需要使用id对JSON字符串进行分组。

JSON 字符串:

[
    "id": 2078012,
    "items": 
        "item": "cos",
        "itemScore": -0.1334,
        "stage": 0.5,
        "reco": 
            "product": "Language",
            "url": "https://",
            "score": 0.019
        
    
,

    "id": 2078012,
    "items": 
        "item": "build",
        "itemScore": -0.2858,
        "stage": 0.5,
        "reco": 
            "product": "Notifications",
            "url": "https://",
            "score": 0.0169
        
    
]

预期输出: 需要使用 ID 对项目进行分组

我已经修改了预期的 JSON,请帮助我创建更新后的 JSON。

[
    "id": 2078012,
    "items": 
        "item": "cos",
        "itemScore": -0.1334,
        "stage": 0.5,
        "reco": [
            "product": "Language",
            "url": "https://",
            "score": 0.019
        ,
        
            "product": "Notifications",
            "url": "https://",
            "score": 0.0169
        ]
    ,
    "items": 
        "item": "build",
        "itemScore": -0.2858,
        "stage": 0.5,
        "reco": [
            "product": "Language",
            "url": "https://",
            "score": 0.019
        ,
        
            "product": "Notifications",
            "url": "https://",
            "score": 0.0169
        ]
    
]

【问题讨论】:

确定你想要这样的输出吗? 'items' 以你想要的方式重复,没有数组表示法......不应该是 "id" : value , "items": [ "item": ... , "item": ... ] 吗? @Samuel Pizarro,JSON 中有一个更正, "id" : value , "items": [ "item": ..., "reco": [ "url" :.....] , "item": ..., "reco": [ "url":.....] ] ,你能帮我创建这个 JSON。 真的吗?我已经正确回答了你原来的问题。现在你把我的答案献给了我,因为你问错了!?呵呵,您可能在开玩笑……我提供了解决原始问题和新问题所需的所有工具。它没有什么不同。在您接受了针对您的问题的有效答案后,您不应更改您的问题。 【参考方案1】:

您可能需要通过几个步骤来打破它。

首先,一个与您的相似的基表。

db2 => SELECT * FROM PROD_T

ID          ITEM                 ITEMSCORE STAGE PRODUCT              URL                  SCORE
----------- -------------------- --------- ----- -------------------- -------------------- -------
    2078012 cos                    -0,1334   0,5 Language             https://              0,0190
    2078012 build                  -0,2585   0,5 Notification         https://              0,1690
    2100000 alpha                  -0,1334   0,5 Letter               https://              0,0190
    2100000 beta                   -0,2585   0,5 anouncement          https://              0,1690

第 1 步:创建基本级别的 json 对象

select id, item, 
  JSON_OBJECT('item' value item, 
               'itemScore' value itemScore,
               'stage' value stage,
               'reco' VALUE JSON_OBJECT('product' value product,
                                        'url' value url ,
                                        'score' value score 
                                        FORMAT JSON ) 
                FORMAT JSON ABSENT ON NULL  
               RETURNING VARCHAR(200) FORMAT JSON) ITEM_JSON  
FROM PROD_T 

ID          ITEM                 ITEM_JSON
----------- -------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    2078012 cos                  "item":"cos","itemScore":-0.1334,"stage":0.5,"reco":"product":"Language","url":"https:\/\/","score":0.0190
    2078012 build                "item":"build","itemScore":-0.2585,"stage":0.5,"reco":"product":"Notification","url":"https:\/\/","score":0.1690
    2100000 alpha                "item":"alpha","itemScore":-0.1334,"stage":0.5,"reco":"product":"Letter","url":"https:\/\/","score":0.0190
    2100000 beta                 "item":"beta","itemScore":-0.2585,"stage":0.5,"reco":"product":"anouncement","url":"https:\/\/","score":0.1690

第 2 步:按 id 分组,并将对象聚合到一个数组中。

WITH BASE AS ( 
select id, item, 
  JSON_OBJECT('item' value item, 
               'itemScore' value itemScore,
               'stage' value stage,
               'reco' VALUE JSON_OBJECT('product' value product,
                                        'url' value url ,
                                        'score' value score 
                                        FORMAT JSON ) 
                FORMAT JSON ABSENT ON NULL  
               RETURNING VARCHAR(200) FORMAT JSON) ITEM_JSON  
FROM PROD_T  ) 
 SELECT ID, JSON_ARRAY ( LISTAGG( ITEM_JSON , ', ') WITHIN GROUP (ORDER BY ITEM) FORMAT JSON ) ITEMS_ARRAY
 FROM BASE GROUP BY ID 


ID          ITEMS_ARRAY
----------- --------------------------------------------------
    2078012 ["item":"build","itemScore":-0.2585,"stage":0.5,"reco":"product":"Notification","url":"https:\/\/","score":0.1690, "item":"cos","itemScore":-0.1334,"stage":0.5,"reco":"product":"Language","url":"https:\/\/","score":0.0190] 
    2100000 ["item":"alpha","itemScore":-0.1334,"stage":0.5,"reco":"product":"Letter","url":"https:\/\/","score":0.0190, "item":"beta","itemScore":-0.2585,"stage":0.5,"reco":"product":"anouncement","url":"https:\/\/","score":0.1690] 

第 3 步:为每个 id 行创建整个 json_object

WITH BASE AS ( 
select id, item, 
  JSON_OBJECT('item' value item, 
               'itemScore' value itemScore,
               'stage' value stage,
               'reco' VALUE JSON_OBJECT('product' value product,
                                        'url' value url ,
                                        'score' value score 
                                        FORMAT JSON ) 
                FORMAT JSON ABSENT ON NULL  
               RETURNING VARCHAR(200) FORMAT JSON) ITEM_JSON  
FROM PROD_T  ) 
 SELECT JSON_OBJECT ( KEY 'id' VALUE ID , 
            KEY 'itens' VALUE 
                   JSON_ARRAY ( LISTAGG( ITEM_JSON , ', ') WITHIN GROUP (ORDER BY ITEM) FORMAT JSON ) 
            FORMAT JSON ) json_objects 
 FROM BASE GROUP BY ID 


json_objects 
-----------------------------------------------------------------------------------------------------------------------------------
"id":2078012,"itens":["item":"build","itemScore":-0.2585,"stage":0.5,"reco":"product":"Notification","url":"https:\/\/","score":0.1690, "item":"cos","itemScore":-0.1334,"stage":0.5,"reco":"product":"Language","url":"https:\/\/","score":0.0190] 
"id":2100000,"itens":["item":"alpha","itemScore":-0.1334,"stage":0.5,"reco":"product":"Letter","url":"https:\/\/","score":0.0190, "item":"beta","itemScore":-0.2585,"stage":0.5,"reco":"product":"anouncement","url":"https:\/\/","score":0.1690]

第 4 步:id 的完整 json doc 数组

下面的查询应该可以工作,

WITH BASE AS ( 
select id, item, 
  JSON_OBJECT('item' value item, 
               'itemScore' value itemScore,
               'stage' value stage,
               'reco' VALUE JSON_OBJECT('product' value product,
                                        'url' value url ,
                                        'score' value score 
                                        FORMAT JSON ) 
                FORMAT JSON ABSENT ON NULL  
               RETURNING VARCHAR(200) FORMAT JSON) ITEM_JSON  
FROM PROD_T  ),  
 PROD_OBJS AS ( 
     SELECT  JSON_OBJECT ( KEY 'id' VALUE ID , 
            KEY 'itens' VALUE 
                   JSON_ARRAY ( LISTAGG( ITEM_JSON , ', ') WITHIN GROUP (ORDER BY ITEM) FORMAT JSON ) 
            FORMAT JSON ) json_objects 
     FROM BASE GROUP BY ID ) 
SELECT JSON_ARRAY (select json_objects FROM PROD_OBJS format json) FROM SYSIBM.SYSDUMMY1

但是,在我的系统 (DB2 v11.5.0.1077) 中,它因一些内部编译错误而失败:

SQL0901N 由于数据库,SQL 语句或命令失败 系统错误。 (原因“意外的 agg opparm”。) SQLSTATE=58004

但是,如果您遇到同样的情况,您可以将 step3 的输出具体化到一个临时表中,并从中执行最终的数组。

DECLARE GLOBAL TEMPORARY TABLE prod_json_objcts AS ( 
WITH BASE AS ( 
select id, item, 
  JSON_OBJECT('item' value item, 
               'itemScore' value itemScore,
               'stage' value stage,
               'reco' VALUE JSON_OBJECT('product' value product,
                                        'url' value url ,
                                        'score' value score 
                                        FORMAT JSON ) 
                FORMAT JSON ABSENT ON NULL  
               RETURNING VARCHAR(200) FORMAT JSON) ITEM_JSON  
FROM PROD_T  ) 
 SELECT JSON_OBJECT ( KEY 'id' VALUE ID , 
            KEY 'itens' VALUE 
                   JSON_ARRAY ( LISTAGG( ITEM_JSON , ', ') WITHIN GROUP (ORDER BY ITEM) FORMAT JSON ) 
            FORMAT JSON ) json_objects 
 FROM BASE GROUP BY ID 
) WITH DATA 

SELECT JSON_ARRAY (select json_objects FROM session.prod_json_objcts format json) FROM SYSIBM.SYSDUMMY1 

生成最终的 json 数组:

["id":2078012,
  "itens":[
      "item":"build",
       "itemScore":-0.2585,
       "stage":0.5,
       "reco":"product":"Notification",
               "url":"https:\/\/",
               "score":0.1690, 
      "item":"cos",
       "itemScore":-0.1334,
       "stage":0.5,
       "reco":"product":"Language",
               "url":"https:\/\/",
               "score":0.0190
    ]
 ,
 "id":2100000,
  "itens":[
      "item":"alpha",
       "itemScore":-0.1334,
       "stage":0.5,
       "reco":"product":"Letter",
               "url":"https:\/\/",
               "score":0.0190, 
        "item":"beta",
         "itemScore":-0.2585,
         "stage":0.5,
         "reco":"product":"anouncement",
                 "url":"https:\/\/",
                 "score":0.1690
    ]
 
]

【讨论】:

Samuel Pizarro 非常感谢您的回答,它正在工作。

以上是关于从java中的Json字符串生成聚合的Json数据的主要内容,如果未能解决你的问题,请参考以下文章

怎样从java后台获取json字符串并转换为json对象输出?

从java中的recippuppy解析json

Java Gson 实现 Json 数据的生成与解析

从 Java Android 中的嵌套 JSON 对象获取数据

java怎么取json数据的值

怎样从java后台获取json字符串并转换为json对象输出?