简体   繁体   English

嵌套 JSON 使用 ms sql 服务器

[英]Nested JSON using ms sql server

I have a table with USER_DATA (user data table) with 2 rows (2 entries basically)我有一个USER_DATA表(用户数据表),有 2 行(基本上 2 个条目)

I created one nested JSON query ->我创建了一个嵌套的 JSON 查询->

SELECT CONCAT( first_name,' ', last_name) AS displayName, 
       first_name AS givenName, last_name AS surname,
       identities = (SELECT login_name AS issuerAssignedId 
                       FROM user_data 
                        FOR JSON AUTO) 
 FROM user_data
  FOR JSON PATH, ROOT('users');

Here, I am getting this output ->在这里,我得到了这个 output ->

{
  "users": [
    {
      "displayName": "David Dave",
      "givenName": "David",
      "surname": "Dave",
      "identities": [
        {
          "issuerAssignedId": "System"
        },
        {
          "issuerAssignedId": "Administrators"
        }       
      ]
    },
    {
      "displayName": "Tony Padila",
      "givenName": "Tony",
      "surname": "Padila",
      "identities": [
        {
          "issuerAssignedId": "System"
        },
        {
          "issuerAssignedId": "Administrators"
        }
      ]
    }

But the problem is -> inside identities,但问题是 -> 内部身份,

"issuerAssignedId": "System" ----> Belongs to Dave "issuerAssignedId": "System" ----> 属于 Dave

"issuerAssignedId": "Administrators" ----> Belongs to Tony "issuerAssignedId": "Administrators" ----> 属于 Tony

But I am not able to stop the inner select query (Not able to map correctly)但我无法停止内部 select 查询(无法正确 map)

The correct output should be --->正确的 output 应该是--->

{
      "users": [
        {
          "displayName": "David Dave",
          "givenName": "David",
          "surname": "Dave",
          "identities": [
            {
              "issuerAssignedId": "System"
            }      
          ]
        },
        {
          "displayName": "Tony Padila",
          "givenName": "Tony",
          "surname": "Padila",
          "identities": [
            {
              "issuerAssignedId": "Administrators"
            }
          ]
        }

PLEASE HELP.请帮忙。

You are missing the condition in the inner query and why do you want the identities to be a separate array in the JSON output.您缺少内部查询中的条件,为什么您希望身份成为 JSON output 中的单独数组。

I have updated the query as per my understanding please refer below sql, I'm not sure why you are having the ** in the query我已根据我的理解更新了查询,请参阅下面的 sql,我不确定您为什么在查询中有**

     SELECT CONCAT (
        FIRST_NAME
        ,' '
        ,LAST_NAME
        ) AS displayName
    ,FIRST_NAME AS givenName
    ,LAST_NAME AS surname
    ,identities = (
        SELECT innr.LOGIN_NAME AS issuerAssignedId
        FROM USER_DATA
        innr
        WHERE
        innr.LOGIN_NAME = ottr.LOGIN_NAME
        FOR JSON AUTO
        )
   FROM USER_DATA ottr
   FOR JSON PATH
      ,ROOT('users');

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

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