简体   繁体   English

HANA DB 中 json_object 的等效函数

[英]Equivalent function in HANA DB for json_object

I would like to return the query results into json format in HANA DB.我想在HANA DB中将查询结果返回为json格式。

There is a json_object function in oracle to achieve this requirement, but I am not seeing any function in HANA. oracle中有一个json_object函数来实现这个要求,但是我在HANA中没有看到任何函数。 Does anyone knows if this kind of function exists in HANA For example:有谁知道HANA中是否存在这种功能例如:

Table Author contains non-json data as follows:表作者包含非json数据如下:

---------------------------------------------
|  firstName        |   lastName            |
---------------------------------------------
|  Paulo            |   Coelho              |
|  George           |   Orwell              |
---------------------------------------------

write a select statement to return result as json.编写一个选择语句以将结果作为 json 返回。 In Oracle it can be returned using query:在 Oracle 中,它可以使用查询返回:

SELECT json_object(
  KEY 'firstName' VALUE author.first_name, 
  KEY 'lastName'  VALUE author.last_name
)
FROM author

Output looks like this:输出如下所示:

---------------------------------------------
| json_array                                 |
---------------------------------------------
| {"firstName":"Paulo","lastName":"Coelho"}  | 
| {"firstName":"George","lastName":"Orwell"} |
----------------------------------------------

Does anyone knows query or function in HANA to achieve the same result?有没有人知道 HANA 中的查询或函数以达到相同的结果?

you can use the already mentioned function in SAP HANA too您也可以在 SAP HANA 中使用已经提到的功能

JSON_QUERY (
 <JSON_API_common_syntax>
 [ <JSON_output_clause> ]
 [ <JSON_query_wrapper_behavior> ]
 [ <JSON_query_empty_behavior> ON EMPTY ]
 [ <JSON_query_error_behavior> ON ERROR ]
  )

research 研究

For 2.0 SP04 and above there's a for json addition to the select statement .对于 2.0 SP04 及更高版本, select语句中添加了一个for json As documentation says, it is only permitted in subqueries, so you need to select individual columns in subselect (if you need a result set of JSON objects) of generate a JSON array as a single scalar result.正如文档所说,它只允许在子查询中使用,因此您需要在 subselect 中选择单个列(如果您需要一个 JSON 对象的结果集)以生成一个 JSON 数组作为单个标量结果。 Column names are inherited from subquery aliases.列名继承自子查询别名。

Case 1:情况1:

with a as (
  select 'AAA' as field1, 'Value 1' as val from dummy union all
  select 'BBB' as field1, 'Value 2' as val from dummy
)
select
  /*Use correlated subquery with single row*/
  json_value((select a.field1, a.val from dummy for json), '$[0]') as res
from a

Or more effort to type-in, but less structure-dependent:或者更努力地输入,但较少依赖于结构:

with a as (
  select 'AAA' as field1, 'Value 1' as val from dummy union all
  select 'BBB' as field1, 'Value 2' as val from dummy
)
, json_source as (
  /*Intermediate query to use as correlation source in JSON_TABLE*/
  select (select * from a for json) as tmp_json
  from dummy
)
select json_parsed.*
from json_source,
  json_table(
  json_source.tmp_json
  /*Access individual items*/
  , '$[*]'
  columns (
    res nvarchar(1000) format json path '$'
  )
) as json_parsed

Both return:两者都返回:

RES RES
{"FIELD1":"AAA","VAL":"Value 1"} {"FIELD1":"AAA","VAL":"值 1"}
{"FIELD1":"BBB","VAL":"Value 2"} {"FIELD1":"BBB","VAL":"值 2"}

Or as a scalar query returning JSON array (Case 2):或者作为返回 JSON 数组的标量查询(案例 2):

with a as (
  select 'AAA' as field1, 'Value 1' as val from dummy union all
  select 'BBB' as field1, 'Value 2' as val from dummy
)
select *
from (select * from a for json)
JSONRESULT JSON结果
[{"FIELD1":"AAA","VAL":"Value 1"},{"FIELD1":"BBB","VAL":"Value 2"}] [{"FIELD1":"AAA","VAL":"值 1"},{"FIELD1":"BBB","VAL":"值 2"}]

That's great to retrieve JSON data from SAP HANA queries.从 SAP HANA 查询中检索 JSON 数据非常棒。

I was wondering if there is a similar solution for a table header + items structure:我想知道对于表头 + 项目结构是否有类似的解决方案:

HEADER:
--------------------------------------
|  ID        |   Name                |
--------------------------------------
|  1         |   Coelho              |
|  2         |   Orwell              |
--------------------------------------

ITEMS:
---------------------------------------------------
|  ID        |   LineID         |   Quantity      |
---------------------------------------------------
|  1         |   1              |   4             |
|  1         |   2              |   3             |
|  2         |   1              |   5             |
---------------------------------------------------

---------------------------------------------------
| OUTPUT (single data row)                        |
---------------------------------------------------
[{"ID": 1, "Name": "Coelho", "Items": [{ "LineID": 1, "Quantity": 4}, { "LineID": 2, "Quantity": 3}]}, {"ID": 2, "Name": "Orwell", "Items": [{ "LineID": 1, "Quantity": 5}]}]  
---------------------------------------------------

I know I can write a SQLScript to achieve same results but is not so simple...我知道我可以编写一个 SQLScript 来实现相同的结果,但不是那么简单......

Any suggestion?有什么建议吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM