SQL Server 2019-使用传播操作员构建JSON

发布于 2025-02-07 07:19:54 字数 1451 浏览 3 评论 0 原文

我需要提供 JSON对象这样:

[
    {
        id: '59E59BC82852A1A5881C082D5FFCAC10',
        user: {
            ...users[1],
            last_message: "16-06-2022",
            topic: "Shipment"
        },
        unread: 2,
    },
    {
        id: '521A754B2BD028B13950CB08CDA49075',
        user: {
            ...users[2],
            last_message: "15-06-2022",
            topic: "Settings"
        },
        unread: 0,
    }
  ]

我很难构建 json 这样:(

使用此小提琴

[
   {
        "id": "59E59BC82852A1A5881C082D5FFCAC10",
        "user": {
            "id": 1,
            "last_message": "16-06-2022",
            "topic": "Shipment"
        },
        "unread": 2
   },
   {
        "id": "521A754B2BD028B13950CB08CDA49075",
        "user": {
            "id": 2,
            "last_message": "15-06-2022",
            "topic": "Settings"
        },
        "unread": 1
   },
   {
        "id": "898BB874D0CBBB1EFBBE56D2626DC847",
        "user": {
            "id": 3,
            "last_message": "18-06-2022",
            "topic": "Account"
        },
        "unread": 1
   }
]

,但我不知道如何将 ...用户[1] ,而不是“ ID”:1 纳入用户 node:

有办法吗?

I'm required to supply a json object like this:

[
    {
        id: '59E59BC82852A1A5881C082D5FFCAC10',
        user: {
            ...users[1],
            last_message: "16-06-2022",
            topic: "Shipment"
        },
        unread: 2,
    },
    {
        id: '521A754B2BD028B13950CB08CDA49075',
        user: {
            ...users[2],
            last_message: "15-06-2022",
            topic: "Settings"
        },
        unread: 0,
    }
  ]

it is not difficult for me to build a json like this:

(with this fiddle https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=bf62626de20d3ca7191aa9c1ef0cd39b)

[
   {
        "id": "59E59BC82852A1A5881C082D5FFCAC10",
        "user": {
            "id": 1,
            "last_message": "16-06-2022",
            "topic": "Shipment"
        },
        "unread": 2
   },
   {
        "id": "521A754B2BD028B13950CB08CDA49075",
        "user": {
            "id": 2,
            "last_message": "15-06-2022",
            "topic": "Settings"
        },
        "unread": 1
   },
   {
        "id": "898BB874D0CBBB1EFBBE56D2626DC847",
        "user": {
            "id": 3,
            "last_message": "18-06-2022",
            "topic": "Account"
        },
        "unread": 1
   }
]

but I have no idea how to put the ...users[1], instead of "id": 1 into user node:

is there a way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2025-02-14 07:19:54

这实际上不是有效的JSON,但是您可以使用 String_agg concat

SELECT
  '[' + STRING_AGG(u.spread, ',') + ']'
FROM (
    SELECT
      spread = CONCAT(
        '{id:''',
        u.userId,
        ''',user:{...users[',
        ROW_NUMBER() OVER (ORDER BY u.id),
        '],last_message: "',
        t.created_at,
        '",topic:"',
        t.topic,
        '"},unread:',
        (SELECT COUNT(*) FROM @tickets t3 WHERE t3.userId = u.userId AND t3.read_at IS NULL),
        '}'
      )
    FROM @Users u
    CROSS APPLY (
        SELECT top 1 
          t.ticketId,
          t.created_at,
          t.topic
        FROM @Tickets t
        WHERE t.userId = u.userId
        ORDER BY
          t.created_at DESC
    ) t
) u

请注意,请注意您可能需要逃脱值,但是我不知道这是如何不 - JSON的工作是不能说的。

This is not actually valid JSON, but you can create it yourself using STRING_AGG and CONCAT

SELECT
  '[' + STRING_AGG(u.spread, ',') + ']'
FROM (
    SELECT
      spread = CONCAT(
        '{id:''',
        u.userId,
        ''',user:{...users[',
        ROW_NUMBER() OVER (ORDER BY u.id),
        '],last_message: "',
        t.created_at,
        '",topic:"',
        t.topic,
        '"},unread:',
        (SELECT COUNT(*) FROM @tickets t3 WHERE t3.userId = u.userId AND t3.read_at IS NULL),
        '}'
      )
    FROM @Users u
    CROSS APPLY (
        SELECT top 1 
          t.ticketId,
          t.created_at,
          t.topic
        FROM @Tickets t
        WHERE t.userId = u.userId
        ORDER BY
          t.created_at DESC
    ) t
) u

Note that you may need to escape values, but I don't know how this not-JSON works so couldn't say.

db<>fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文