DEV Community

Cover image for Querying JSON and its nested objects with TSQL
Nirmal Krishna
Nirmal Krishna

Posted on

5 2

Querying JSON and its nested objects with TSQL

When we were faced to store lot of dynamically modelled data in a relational DB, we decided on storing them in a JSON column of SQL server.

The well documented recourses came in handy during initial development.

Although with some workaround

  • Runtime JSON deserialization
  • Getting around with Store procedures while whole application runs with entity framework

Until today I never got to query nested arrays of a JSON object with SQL. That is when I stumbled upon another method in T-SQL called OPENJSON

OPENJSON is a table-valued function that parses JSON text and returns objects and properties from the JSON input as rows and columns.

In other words, OPENJSON provides a rowset view over a JSON document. You can explicitly specify the columns in the rowset and the JSON property paths used to populate the columns. Since OPENJSON returns a set of rows, you can use OPENJSON in the FROM clause of a T-SQL statement just as you can use any other table, view, or table-valued function.

Few samples

Consider this sample json data



DECLARE @json NVARCHAR(MAX) = '{
    "firstName":"James",
    "lastName":"Doe",
    "ChildrenDetails":[
        {
            "Id":1,
            "Name":"John Doe",
            "Languages":["English", "German"]
        },
        {
            "Id":2,
            "Name":"Jane Doe",
            "Languages":["English"]
        },
        {
            "Id":3,
            "Name":"June Doe",
            "Languages":["German", "Tamil"]
        }]
}'


Enter fullscreen mode Exit fullscreen mode

1. Querying into arrays (ChildrenDetails)



SELECT 
    JSON_VALUE(d.value,'$.Id') AS Id,
    JSON_VALUE(d.value,'$.Name') AS Name
FROM OPENJSON(@json,'$.ChildrenDetails') AS d


Enter fullscreen mode Exit fullscreen mode

Alt Text

2. Flatten Nested arrays (ChildrenDetails + each language)



SELECT 
JSON_VALUE(d.value,'$.Id') AS Id,
JSON_VALUE(d.value,'$.Name') AS Languages,
l.value AS Name
  FROM OPENJSON(@json,'$.ChildrenDetails') AS d CROSS APPLY OPENJSON (d.value,'$.Languages') AS l


Enter fullscreen mode Exit fullscreen mode

Alt Text

Hopefully you can try this with complex JSON structures. The use cases are many, but today this came in handy for a feature to us πŸ˜ƒ

Before you leave, a meme:
Alt Text

EOF

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay