SQL to JSON

fallows1980

New member
Joined
Dec 23, 2020
Messages
1
Programming Experience
Beginner
Hi

I have a stored procedure which returns data like:

SQL:
CREATE TABLE [dbo].[test](
    [date1] [datetime] NULL,
    [number] [int] NOT NULL,
    [id] [int] NULL,
    [Description] [varchar](max) NULL,
    [date2] [datetime] NULL,
    [rowVersion] [nvarchar](max) NULL,
    [date3] [datetime] NULL,
    [id2] [int] NULL,
    [code] [varchar](10) NULL,
    [type] [varchar](10) NULL,
    [date4] [datetime] NULL,
    [Owned] [varchar](3) NULL,
    [RowVersion2] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
INSERT [dbo].[test] ([date1], [number], [id], [Description], [date2], [rowVersion], [date3], [id2], [code], [type], [date4], [Owned], [RowVersion2])

VALUES (CAST(N'2018-11-09T17:57:42.220' AS DateTime), 123456, 5566, N'Full description', CAST(N'2018-11-09T17:57:42.220' AS DateTime), N'0x000000000T49G19B', CAST(N'2018-12-27T14:21:42.570' AS DateTime), 1001, N'101', N'1', CAST(N'2020-01-24T16:17:09.287' AS DateTime), N'Yes', N'0x000000000E1TST89')
GO
INSERT [dbo].[test] ([date1], [number], [id], [Description], [date2], [rowVersion], [date3], [id2], [code], [type], [date4], [Owned], [RowVersion2])

VALUES (CAST(N'2018-11-09T17:57:42.220' AS DateTime), 123456, 5566, N'Full description', CAST(N'2018-11-09T17:57:42.220' AS DateTime), N'0x000000000T49G19B', CAST(N'2018-12-27T14:21:42.570' AS DateTime), 1004, N'202', N'2', CAST(N'2020-01-24T16:17:09.287' AS DateTime), N'Yes', N'0x000000000E1TST89')
GO
INSERT [dbo].[test] ([date1], [number], [id], [Description], [date2], [rowVersion], [date3], [id2], [code], [type], [date4], [Owned], [RowVersion2])

VALUES (CAST(N'2018-11-09T17:57:42.220' AS DateTime), 123456, 5566, N'Full description', CAST(N'2018-11-09T17:57:42.220' AS DateTime), N'0x000000000T49G19B', CAST(N'2018-12-27T14:21:42.570' AS DateTime), 1010, N'303', N'3', CAST(N'2020-01-24T16:17:09.287' AS DateTime), N'Yes', N'0x000000000E1TST89')
GO


I'm trying to return the data in the following format:

JSON:
{
    "section1": {
        "date1": "2018-11-09",
        "id": "5566",
        "Description": "Full description",
        "date2": "2018-11-09T17:57:42",
        "rowVersion": "0x000000000T49G19B"
    },
    "section2": {
        "date3": "2018-11-09",
        "codes": [
            {
                "id2": "1001",
                "Code": "101",
                "Type": "1"
            },
            {
                "id2": "1004",
                "Code": "202",
                "Type": "2"
            },
            {
                "id2": "1010",
                "Code": "303",
                "Type": "3"
            }
        ]
    },
    "section3": {
        "date4": "2020-01-24",
        "owned": "true",
        "rowVersio2": "0x000000000E1TST89"
    }
}

So far I have retrieved the data in a dataset but i'm struggling with the JSON conversion.

I have tried
C#:
var serializedMyObjects = JsonConvert.SerializeObject(sqlResults.Tables[0]);
            var returnData = JsonConvert.DeserializeObject<Activities[]>(serializedMyObjects);

but this is not giving me the desired results. Can anyone point me in the right direction?

Thank
Paul
 
Back
Top Bottom