I have the following JSON String stored in table
create table #t1 (Caseno int, CaseText text)
insert into #t1 values
(1,'[{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PCNForEnrollmentCreationDesc","DisplayName":"Header","FieldSchemaName":null,"FieldValue":"","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"H2","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PrimaryEntityInformation","DisplayName":"Primary Entity Information","FieldSchemaName":null,"FieldValue":"","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityName","DisplayName":"Primary Entity Name","FieldSchemaName":null,"FieldValue":"Mau \"Informatsionnye tekhnologii\"","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityAddress","DisplayName":"Primary Entity Address","FieldSchemaName":null,"FieldValue":"123 Main St, ","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityCity","DisplayName":"Primary Entity City","FieldSchemaName":null,"FieldValue":"Kaliningrad","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityPhone","DisplayName":"Primary Entity Phone","FieldSchemaName":null,"FieldValue":"772 322-977","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityPostalCode","DisplayName":"Primary Entity Postal Code","FieldSchemaName":null,"FieldValue":"236029","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityCountry","DisplayName":"Primary Entity Country","FieldSchemaName":null,"FieldValue":"Russia","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"H2","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PrimaryContactInformation","DisplayName":"Primary Contact Information","FieldSchemaName":null,"FieldValue":"","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollContactTypeIndividual","DisplayName":"Contact Type","FieldSchemaName":null,"FieldValue":"Individual","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContactFirstName","DisplayName":"Given Name: ","FieldSchemaName":null,"FieldValue":"Elena","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContactLastName","DisplayName":"Family Name: ","FieldSchemaName":null,"FieldValue":"Andre","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityCountry","DisplayName":"Primary Entity Country","FieldSchemaName":null,"FieldValue":"Russia","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityAddress","DisplayName":"Primary Entity Address","FieldSchemaName":null,"FieldValue":"123 Main St, ","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityCity","DisplayName":"Primary Entity City","FieldSchemaName":null,"FieldValue":"Kaliningrad","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityPhone","DisplayName":"Primary Entity Phone","FieldSchemaName":null,"FieldValue":"772 322-977","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContEmailAddress","DisplayName":"Email Address","FieldSchemaName":null,"FieldValue":"info#us.me","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContDataLanguage","DisplayName":"Data Language","FieldSchemaName":null,"FieldValue":"Russkij","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PCNforEnrollPrimaryEntity","DisplayName":"PCN for Enrollment Primary Entity","FieldSchemaName":null,"FieldValue":"123455","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":"FreeText","IsRequired":"true"},{"RowType":"H2","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"NoticesEntityInformation"')
I want to extract the value from the string and present this way:
Caseno FieldName FieldValue
1 Header
1 Primary Entity Information
1 Primary Entity Name Mau \"Informatsionnye tekhnologii
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Primary Entity Postal Code 236029
1 Primary Entity Country Russia
1 Primary Contact Information
1 Contact Type Individual
1 Given Name: Elena
1 Family Name: Andre
1 Primary Entity Country Russia
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Email Address info#us.me
1 Data Language Russkij
1 PCN for Enrollment Primary Entity 123455
Basically I want to extract DisplayName and FieldValues.
Example String: "DisplayName":"Primary Entity Name","FieldSchemaName":null,"FieldValue":"Mau \"Informatsionnye tekhnologii\""
If you use SQL Server 2016+, just parse the JSON with OPENJSON() using explicit schema (WITH clause). The data in the question is truncated (and the JSON is not valid), but the following example is a working solution:
Table and JSON (fixed and simplified):
create table #t1 (Caseno int, CaseText text)
insert into #t1 values
(1,'[
{
"RowType":"KV",
"s1":0,
"s2":0,
"s3":0,
"TableNo":0,
"FieldName":"PCNForEnrollmentCreationDesc",
"DisplayName":"Header",
"FieldSchemaName":null,
"FieldValue":"",
"IsDynamic":true,
"IsDependent":false,
"DependentKey":null,
"IsRecurrence":false,
"RecurrenceKey":null,
"Section":null,
"Table":null,
"DataType":null,
"UIOrder":2,
"LookupSelectField":null,
"LookupFilterField":null,
"col3":null,
"col4":null,
"col5":null,
"col6":null,
"col7":null,
"col8":null,
"col9":null,
"col10":null,
"col11":null,
"col12":null,
"col13":null,
"FieldType":null,
"IsRequired":null
},
{
"RowType":"H2",
"s1":0,
"s2":0,
"s3":0,
"TableNo":0,
"FieldName":"PrimaryEntityInformation",
"DisplayName":"Primary Entity Information",
"FieldSchemaName":null,
"FieldValue":"",
"IsDynamic":false,
"IsDependent":false,
"DependentKey":null,
"IsRecurrence":false,
"RecurrenceKey":null,
"Section":null,
"Table":null,
"DataType":null,
"UIOrder":2,
"LookupSelectField":null,
"LookupFilterField":null,
"col3":null,
"col4":null,
"col5":null,
"col6":null,
"col7":null,
"col8":null,
"col9":null,
"col10":null,
"col11":null,
"col12":null,
"col13":null,
"FieldType":null,
"IsRequired":null
},
{
"RowType":"KV",
"s1":0,
"s2":0,
"s3":0,
"TableNo":0,
"FieldName":"EnrollPrimaryEntityName",
"DisplayName":"Primary Entity Name",
"FieldSchemaName":null,
"FieldValue":"Mau \"Informatsionnye tekhnologii\"",
"IsDynamic":true,
"IsDependent":false,
"DependentKey":null,
"IsRecurrence":false,
"RecurrenceKey":null,
"Section":null,
"Table":null,
"DataType":null,
"UIOrder":2,
"LookupSelectField":null,
"LookupFilterField":null,
"col3":null,
"col4":null,
"col5":null,
"col6":null,
"col7":null,
"col8":null,
"col9":null,
"col10":null,
"col11":null,
"col12":null,
"col13":null,
"FieldType":null,
"IsRequired":null
}
]')
Statement:
SELECT t.CaseNo, j.*
FROM #t1 t
CROSS APPLY OPENJSON(t.CaseText) WITH (
DisplaydName varchar(100) '$.DisplayName',
FieldValue varchar(100) '$.FieldValue'
) j
Result:
CaseNo DisplaydName FieldValue
1 Header
1 Primary Entity Information
1 Primary Entity Name Mau "Informatsionnye tekhnologii"
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Primary Entity Postal Code 236029
1 Primary Entity Country Russia
1 Primary Contact Information
1 Contact Type Individual
1 Given Name: Elena
1 Family Name: Andre
1 Primary Entity Country Russia
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Email Address info#us.me
1 Data Language Russkij
1 PCN for Enrollment Primary Entity 123455