How can I make my json sql query dynamic

Funke Paul 20 Reputation points
2024-10-28T10:50:31.87+00:00

Hello team of experts,

I have a field named MEASUREMENT_DATA_CONFIG in one of my tables named PROCESS_T that contains json code in my ms sql database catalog. Unfortunately I am not able to read the data dynamically in a clean way. If I specify the location in the array e.g. [0] then I can read the value, but the number of values 'Sections' and the number of values 'Measurements' is more than 1 (i.e. 1 to n). Is there a possibility to read out these values dynamically, i.e. instead of just returning the array position [0], select all available positions. If so, what could such a query look like?Example Query

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,654 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 118K Reputation points
    2024-10-28T16:13:27.63+00:00

    To display the arrays vertically, try to adjust this query:

    select PROCESS_ID, Name, Version, t2.S_Token, t3.*
    from process_t
    cross apply openjson(MEASUREMENT_DATA_CONFIG) with ( Sections nvarchar(max) '$.Sections' as json) t1
    cross apply openjson(t1.Sections) with ( 
    	S_Token nvarchar(max) '$.Token',
    	Measurments nvarchar(max) '$.Measurements' as json) t2
    cross apply openjson(t2.Measurments) with ( 
    	M_Token nvarchar(max) '$.Token',
    	M_Name nvarchar(max) '$.Name',
    	M_IsReportable nvarchar(max) '$.IsReportable') t3
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.