0

I am trying to develop database table structure from following JSON Structure. "requiredfields" are straight forward and I table setup for those. However, I am stuck on how to created schema to store "Configuration" and its nested properties in SQL Database. Any help will be appreciated. Thanks.

{
    "requiredFields": [
        "hello",
        "world"
    ],
    "configuration": {
        "hello": {
            "fallbacks": [
                {
                    "type": "constant",
                    "value": "30"
                }
            ]
        },
        "world": {
            "fallbacks": [
                {
                    "type": "fromInputFile",
                    "value": "patientFirstName"
                },
                {
                    "type": "fromInputFile",
                    "value": "subscriberFirstName"
                },
                {
                    "type": "constant",
                    "value": "alpha"
                }
            ]
        }
    }
}

1 Answer 1

1

It looks like you could store that in a single table

CREATE TABLE ConfigurationFallbacks (
    field nvarchar(100),
    type nvarchar(100),
    value nvarchar(100)
);

You may also want to add another table to store just the field values, and the table above would be foreign-keyed to that.

You can insert like this:

INSERT ConfigurationFallbacks (field, type, value)
SELECT
  keys.[key],
  j.type,
  j.value
FROM OPENJSON(@json, '$.configuration') keys
CROSS APPLY OPENJSON(keys.value, '$.fallbacks')
  WITH (
    type nvarchar(100),
    value nvarchar(100)    
  ) j;

The first OPENJSON call does not have a schema, so it returns a set of key value pairs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.