json file in sql server

Vineet S 145 Reputation points
2024-03-01T02:01:10.4466667+00:00

could you please explain how we can load below curly bracket files in sql server when there are multiple unwanted curly brackets are there apart from main data?

in this case data is available in till Result only

{
    "Generic": {
        "id": "33",
        "Products": [
            {
                "Code": "111",
                "Amount": 1.0,
                "category": "33",
                "price": 11,
                "totalprice": 233
            }
        ],
        "Result": "test",
    },
    "Notification": {
        "Environment": "local",
        "Instance": "local",
        "Time": "00"}
Azure SQL Database
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,737 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,454 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,245 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 24,946 Reputation points
    2024-03-01T03:04:32.5733333+00:00

    Hi @Vineet S,

    Please try the following solution.

    -- INSERT INTO tableName -- uncomment when you are ready
    SELECT j1.id, j1.Result, prod.*
    FROM OPENROWSET(BULK 'e:\temp\vineet.json', SINGLE_CLOB) AS j
        CROSS APPLY
        OPENJSON(BulkColumn,'$.Generic')
        WITH
        (
            id			INT '$.id'
          , Result		NVARCHAR(30) '$.Result'
          , Products	NVARCHAR(max) AS JSON
        ) AS j1
    CROSS APPLY (
        SELECT *
        FROM OPENJSON(j1.Products)
        WITH
        (
    		Code		VARCHAR(50)		'$.Code',
    		Amount		DECIMAL(10,2)	'$.Amount',
    		category	INT				'$.category',
    		price		DECIMAL(10,2)	'$.price',
    		totalprice	DECIMAL(10,2)	'$.totalprice'
        ) j2
    ) AS prod;
    
    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful