5

So let's say I have a varchar column in a table with some structure like:

{
   "Response":{
      "DataArray":[
         {
            "Type":"Address",
            "Value":"123 Fake St"
         },
         {
            "Type":"Name",
            "Value":"John Doe"
         }
      ]
   }
}

And I want to create a persisted computed column on the "Value" field of the "DataArray" array element that contains a Type field that equals "Name". (I hope I explained that properly. Basically I want to index the people names on that structure).

The problem is that, unlike with other json objects, I can't use the JSON_VALUE function in a straightforward way to extract said value. I've no idea if this can be done, I've been dabbling with JSON_QUERY but so far I've no idea what to do.

Any ideas and help appreciated. Thanks!

4
  • Is there any way you could pre-process the JSON in your application code to extract the value beforehand?
    – Dai
    Sep 17, 2018 at 18:41
  • Yes. Unfortunately the application layer does basically nothing with the json but insert it and people would like to keep it that way if possible. I've no idea if this is possible so I have to do my research first. That's not my call and neither is restructuring the json (since the Types are always the same we should have them as fields, not with this array weird data structure).
    – Gaspa79
    Sep 17, 2018 at 18:47
  • You could also hack something using CHARINDEX too.
    – Dai
    Sep 17, 2018 at 18:54
  • 1
    @Dai There is no need for string processing with CHARINDEX. SQL Server is perfectly capable of handling JSON. Sep 27, 2018 at 19:52

3 Answers 3

10
+50

You could achieve it using function:

CREATE FUNCTION dbo.my_func(@s NVARCHAR(MAX))
RETURNS NVARCHAR(100)
WITH SCHEMABINDING
AS
BEGIN
   DECLARE @r NVARCHAR(100);

   SELECT @r = Value 
   FROM OPENJSON(@s,'$.Response.DataArray')  
   WITH ([Type] NVARCHAR(100) '$.Type', [Value] NVARCHAR(100) '$.Value')
   WHERE [Type] = 'Name';

   RETURN @r;
END;

Defining table:

CREATE TABLE tab(
  val NVARCHAR(MAX) CHECK (ISJSON(val) = 1),
  col1 AS dbo.my_func(val) PERSISTED        -- calculated column
);

Sample data:

INSERT INTO tab(val) VALUES (N'{
   "Response":{
      "DataArray":[
         {
            "Type":"Address",
            "Value":"123 Fake St"
         },
         {
            "Type":"Name",
            "Value":"John Doe"
         }
      ]
   }
}');

CREATE INDEX idx ON tab(col1);   -- creating index on calculated column

SELECT * FROM tab;

db<>fiddle demo

1
  • 1
    Yup, this is the one. I upvoted everyone's answer but I'm using this. Thanks a lot. I learnt a lot from this post.
    – Gaspa79
    Sep 28, 2018 at 14:28
2

You could use a computed column with PATINDEX and index that:

CREATE TABLE foo (a varchar(4000), a_ax AS (IIF(PATINDEX('%bar%', a) > 0, SUBSTRING(a, PATINDEX('%bar%', a), 42), '')))

CREATE INDEX foo_x ON foo(a_ax)
1

You could use a scalar function as @Lukasz Szozda posted - it's a good solution for this. The problem, however, with T-SQL scalar UDFs in computed columns is that they destroy the performance of any query that table is involved in. Not only does data modification (inserts, updates, deletes) slow down, any execution plans for queries that involve that table cannot leverage a parallel execution plan. This is the case even when the computed column is not referenced in the query. Even index builds lose the ability to leverage a parallel execution plan. Note this article: Another reason why scalar functions in computed columns is a bad idea by Erik Darling.

This is not as pretty but, if performance is important than this will get you the results you need without the drawbacks of a scalar UDF.

CREATE TABLE dbo.jsonStrings
(
  jsonString VARCHAR(8000) NOT NULL, 
  nameTxt AS (
    SUBSTRING(
      SUBSTRING(jsonString,
        CHARINDEX('"Value":"',jsonString,
          CHARINDEX('"Type":"Name",',jsonString,
            CHARINDEX('"DataArray":[',jsonString)+12))+9,8000),1,
      CHARINDEX('"', 
        SUBSTRING(jsonString,
          CHARINDEX('"Value":"',jsonString,
            CHARINDEX('"Type":"Name",',jsonString,
              CHARINDEX('"DataArray":[',jsonString)+12))+9,8000))-1)) PERSISTED
);

INSERT dbo.jsonStrings(jsonString)
VALUES
('{
   "Response":{
      "DataArray":[
         {
            "Type":"Address",
            "Value":"123 Fake St"
         },
         {
            "Type":"Name",
            "Value":"John Doe"
         }
      ]
   }
}');

Note that, this works well for the structure you posted. It may need to be tweaked depending on what the JSON does and can look like.

A second (and better but more complex) solution would be to take the json path logic from Lukasz Szozda's scalar UDF and get it into a CLR. T-SQL scalar UDFs, when written correctly, do not have the aforementioned problems that T-SQL scalar UDFs do.

1
  • 1
    This is a very complete post. Thank you. I'll keep the parallel execution drawback in mind. I can't do anything about it, but this is great to know. If this comes back to haunt me in the future I'll try the optimization you suggested. I'd upvote you 10 times if I could.
    – Gaspa79
    Sep 28, 2018 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.