1

I need some help extracting the data I need out of a JSON variable in SQL:

JSON Tree

I need to get management_account_id, Month and cost_to_client/details in a table, the problem I am facing is that the month is higher up on the hierarchy than the details section, so I'm going into each individual month to access the details object, below is the code I have:

/* management_account_id, [cost_to_client] 8 fields */

SELECT table2.ACCID, 
h3.[Management fees received],
h3.[Ad hoc invoices]
FROM OPENJSON(@WP_ACCOUNT, '$.result') j1
OUTER APPLY OPENJSON(j1.[value]) WITH (
    ACCID nvarchar(50) '$.management_account_id',
    cost_to_client nvarchar(max) '$.cost_to_client' as JSON
) table2

CROSS APPLY OPENJSON(table2.cost_to_client)
    with(
        [December 2020] nvarchar(max) as JSON,
        [January 2021] nvarchar(max) as JSON,
        [February 2021] nvarchar(max) as JSON,
        [March 2021] nvarchar(max) as JSON,
        [April 2021] nvarchar(max) as JSON,
        [May 2021] nvarchar(max) as JSON,
        [June 2021] nvarchar(max) as JSON,
        [July 2021] nvarchar(max) as JSON
    ) h1

/* Level 2: Month expanded  */

CROSS APPLY OPENJSON(h1.[December 2020])
    with(
        details nvarchar(max) as JSON
    ) h2

/* Level 3: Month/details expanded  */

CROSS APPLY OPENJSON(h2.details)
    with(
        [Management fees received] nvarchar(50),
        [Ad hoc invoices] nvarchar(50)
    ) h3
;

This is what I'm getting:

Data Snippet

What I want instead is the below which has all the months in it:

Output

JSON in text below:

{
    "result": {
        "12183": {
            "management_account_id": "12183",
            "label": "The Zone",
            "waste_stream_statistics": [],
            "rebates": [],
            "cost_to_client": {
                "December 2020": {
                    "value": 62432.78,
                    "details": {
                        "Management fees received": 62432.78
                    }
                },
                "January 2021": {
                    "value": 62432.78,
                    "details": {
                        "Management fees received": 62432.78
                    }
                },
                "February 2021": {
                    "value": 62432.78,
                    "details": {
                        "Management fees received": 62432.78
                    }
                },
                "March 2021": {
                    "value": 62432.78,
                    "details": {
                        "Management fees received": 62432.78
                    }
                },
                "April 2021": {
                    "value": 62432.78,
                    "details": {
                        "Management fees received": 62432.78
                    }
                },
                "May 2021": {
                    "value": 62432.78,
                    "details": {
                        "Management fees received": 62432.78
                    }
                },
                "June 2021": {
                    "value": 62432.78,
                    "details": {
                        "Management fees received": 62432.78
                    }
                },
                "July 2021": {
                    "value": 62432.78,
                    "details": {
                        "Management fees received": 62432.78
                    }
                }
            }
        }
    }   
}   
5
  • 2
    Can you post sample JSON data as text? Thanks.
    – Zhorov
    Commented Jul 14, 2021 at 13:00
  • 1
    Added in question at the bottom Commented Jul 14, 2021 at 13:10
  • So the problem si it isnt clear on why you want to select the record youre showing us as the desired result. Are you need to get the first record from december by date? or first record from eahc month by date? It would help us to help you if we had clearer requirements
    – Doug Coats
    Commented Jul 14, 2021 at 13:13
  • Are you saying you want a running total of all months, but show the last month (december)? That seems to be what your excel example output is showing
    – Aron
    Commented Jul 14, 2021 at 13:16
  • I want the records from each month displayed in a table , in my query I am showing that I am manually going into the month ie, being December to access details. I don't want this, I would like to know if there is a way to instead pull details for all months. Commented Jul 14, 2021 at 13:17

1 Answer 1

1

If I understand you correctly and you want to avoid explicit columns definitions for the $."cost_to_client" part of the input JSON, the following statement is an option:

SELECT 
   j2.ACCID,
   h1.[key] AS [Month],
   h2.[Management fees received]
FROM OPENJSON(@WP_ACCOUNT, '$.result') j1
OUTER APPLY OPENJSON(j1.[value]) WITH (
   ACCID nvarchar(50) '$.management_account_id',
   cost_to_client nvarchar(max) '$.cost_to_client' AS JSON
) j2
CROSS APPLY OPENJSON(j2.cost_to_client) h1
CROSS APPLY OPENJSON(h1.[value]) WITH (
   [value] numeric(10, 2) '$.value',
   [Management fees received] numeric(10, 2) '$.details."Management fees received"'
) h2

Result:

ACCID Month         Management fees received
--------------------------------------------
12183 December 2020 62432.78
12183 January 2021  62432.78
12183 February 2021 62432.78
12183 March 2021    62432.78
12183 April 2021    62432.78
12183 May 2021      62432.78
12183 June 2021     62432.78
12183 July 2021     62432.78

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