locked
Rename JSON Column RRS feed

  • Question

  • I have a query that outputs a JSON string.

    SELECT xxx, yyy, zzz FROM Table1 FOR JSON PATH, ROOT('my_data'). 

    Works perfectly but it names the column 'JSON_(plus about 15 digits)'.

    Is there anyway to rename column to something like MyJSON.

    Tuesday, April 28, 2020 10:27 PM

Answers

  • Hi there,

    Please try the following:

    SELECT (
    	SELECT *
    	FROM Table1 FOR JSON PATH, ROOT('my_data')
    ) AS [MyJSON];

    • Proposed as answer by Naomi N Wednesday, April 29, 2020 2:01 AM
    • Marked as answer by mogulman52 Wednesday, April 29, 2020 4:58 PM
    Tuesday, April 28, 2020 10:52 PM

All replies

  • Hi there,

    Please try the following:

    SELECT (
    	SELECT *
    	FROM Table1 FOR JSON PATH, ROOT('my_data')
    ) AS [MyJSON];

    • Proposed as answer by Naomi N Wednesday, April 29, 2020 2:01 AM
    • Marked as answer by mogulman52 Wednesday, April 29, 2020 4:58 PM
    Tuesday, April 28, 2020 10:52 PM
  • Hi mogulman52,

    declare @j nvarchar(max)=(SELECT xxx, yyy, zzz FROM Table1 FOR JSON PATH, ROOT('my_data'))
    select @j as MyJSON

    Please refer to: How to change JSON Column name.

    How to change SQL Server for JSON Auto column name?

    Best Regards,

    Lily


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com


    • Edited by Lily Lii Wednesday, April 29, 2020 1:45 AM
    • Proposed as answer by Naomi N Wednesday, April 29, 2020 2:01 AM
    Wednesday, April 29, 2020 1:29 AM
  • Thanks Yitzhak, worked perfectly.
    Wednesday, April 29, 2020 5:01 PM