How To Process JSON Columns, having '.' (dot) in Their Names?

  • Cross post of Stack Overflow as well, for future readers.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thom A wrote:

    I can't keep on giving answers on moving goal posts. I have shown how you would do this, I will leave it to you to implement.

    JSON_VALUE(v.value, '$.fields.System.State.newValue') will not work though, as there is that is not reference fields."System.State".newValue (" added for clarity), it's trying to reference the element State in the element System ; neither of which exist.

    How come JSON_VALUE(v.value, '$.fields.MainValue.newValue') returns a value?

  • BOR15K wrote:

    How come JSON_VALUE(v.value, '$.fields.MainValue.newValue') returns a value?

    Because that exists in yoru data.... There is no element called "System" not "State", you have one called "System.State" and are trying to reference it as separate elements, as I have explain to you... If we use brackets ([]) to demonstrate instead, "System.State" does not mean "[System.State]"... It would mean "[System].[State]", so "$.fields.System.State.newValue" would mean "$.[fields].[System].[State].[newValue]" not "$.[fields].[System.State].[newValue]".

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • The solution for anyone, who might run into the same issue is to place double quotes on the element with the dot, so it works perfect for me now, thank for a help from  here https://stackoverflow.com/questions/64140092/t-sql-how-to-process-json-elements-with-dot-in-their-names

     

    DECLARE @v_Json NVARCHAR(MAX) = N'{
    "count": 56,
    "value": [{
    "id": 1,
    "workItemId": 1234,
    "fields": {
    "MainValue": {"newValue": 98765},
    "System.UserName": "User Name 1",
    "System.Id": {
    "newValue": 1234
    },
    "System.State": {
    "newValue": "New"
    }
    }
    },
    {
    "id": 2,
    "workItemId": 1234,
    "fields": {
    "MainValue": {"newValue": 123456, "oldValue": 98765},
    "System.UserName": "User Name 2",
    "System.State": {
    "oldValue": "new",
    "newValue": "Defined"
    }
    }
    }
    ]
    }';



    SELECTJSON_VALUE(v.value, '$.id') AS id,
    JSON_VALUE(v.value, '$.workItemId') AS workItemID,
    JSON_VALUE(v.value, '$.fields.MainValue.newValue') AS MainValue,
    JSON_VALUE(v.value, '$.fields."System.UserName"') AS itemUserName,
    JSON_VALUE(v.value, '$.fields."System.State".newValue') AS itemStateNewValue

    FROM OPENJSON(@v_Json, '$.value') AS v

Viewing 4 posts - 16 through 18 (of 18 total)

You must be logged in to reply to this topic. Login to reply