Unable to extract useful info from JSON data stored in table.
I have JSON data stored in a SQL Server table which I would like to access one or two elements to check values etc. My table is defined as below:
CREATE TABLE [Events].[events](
[eventID] [int] IDENTITY(1,1) NOT NULL,
[eventType] [smallint] NOT NULL,
[eventDate] [datetime] NOT NULL,
[eventMetaData] [nvarchar](max) NULL,
CONSTRAINT [PK_events] PRIMARY KEY CLUSTERED
(
[eventID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
with the JSON data being held in the eventMetaData col.
The eventMetaData column stores the data as follows:
[{
"jn": "jn1",
"src": "client123",
"id": 649
}]
and I'd like to try to extract this info for display on a page.
So far I've tried to use the JSON_VALUE and JSON_QUERY methods of extracting the required string although I believe its the JSON_VALUE one I should be using.
SELECT top 10 eventId, eventDate,
JSON_VALUE(eventData,'$.jn') AS jn
from events
WHERE ISJSON(eventData)>0
order by eventdate desc
When I run this I get the eventID & eventDate but always get NULL in the jn col.
Some posts have shown examples of the data being in a variable of type nvarchar(max), and if I put my JSON into this it works with my query but I really need it to come from the table as I need to display multiple rows to my customer.
From the example I posted above I'd like to get the eventID, eventDate & jn values output from the query but at the minute I'm getting the eventID & eventDate but always get NULL in the jn col.
Ultimately I'm hoping to have more keys within the JSON col which I can access and display to the end user.