Jump to content

SQL Server 2016 Json parsing super man...


loveindia

Recommended Posts

If you are using Json and SQL Server 2016, its an awesome combo man... I am using it hands on currently and super experience so far... just a quick example for newbies interested in this...

	DECLARE @Json NVARCHAR(MAX)
	SET @Json = '{"requests": [
	    {
	      "id": 0,
	      "data": [
	        {
	          "id": 0,
	          "field": "customerAddress",
	          "stringValue": "2301 Perf. Drive"
	        }
	      ],
	      "where": [
	        {
	          "id": 0,
	          "field": "customerid",
	          "intValue": 1
	        },
	        {
	          "id": 1,
	          "field": "isActive",
	          "stringValue": "1"
	        }
	      ],
	      "table": "custAddress"
	    },
	    {
	      "id": 1,
	      "data": [
	        {
	          "id": 0,
	          "field": "customerAddress",
	          "stringValue": "111 West Irving"
	        }
	      ],
	      "where": [
	        {
	          "id": 0,
	          "field": "customerid",
	          "intValue": 2
	        },
	        {
	          "id": 1,
	          "field": "isActive",
	          "stringValue": "1"
	        }
	      ],
	      "table": "custAddress"
	    }
	    ]
	}    '
	;WITH CTE AS
	(
	SELECT *
	FROM OpenJson(@Json, '$.requests')
	)
	SELECT a.[key]
	    , JSON_VALUE(b.[value], '$.field') AS update_field
	    , JSON_VALUE(b.[value], '$.stringValue') AS update_value
	    , JSON_VALUE(c.[value], '$.field') AS where_field
	    , ISNULL(JSON_VALUE(c.[value], '$.stringValue'), JSON_VALUE(c.[value], '$.intValue')) AS where_value
	FROM CTE a
	CROSS APPLY OpenJson(a.[value], '$.data') b
	CROSS APPLY OpenJson(a.[value], '$.where') c
	

Link to comment
Share on other sites

  • Replies 43
  • Created
  • Last Reply

Top Posters In This Topic

  • loveindia

    17

  • k2s

    3

  • Kontekurradu

    3

  • yomama

    3

Top Posters In This Topic

Just now, loveindia said:

non - relational representation of data man... similar to xml but more advanced and easy to understand.. 15xx4n.gif

Ankulu naaku SQL nerpisthava ? i am serious 

Link to comment
Share on other sites

3 minutes ago, yomama said:

Uncle endidi what's this parsing

parsing antey analyzing the data, so JSON to sql result set techukoni we will analyze man... 15xx4n.gif

Link to comment
Share on other sites

Json with ms sql enduku..licensing ki money waste....mongodb bettrr....app prgramming open source vundu mallu ..sql enduku licnese ki money spend cheyyali

Link to comment
Share on other sites

Just now, sqladdict said:

Json with ms sql enduku..licensing ki money waste....mongodb bettrr....app prgramming open source vundu mallu ..sql enduku licnese ki money spend cheyyali

lol, company ki license konatam valla tax tagguddi and moreover flexibility to use their data will increase man... mongodb enti ye tech lo aina cheyochu whatever you want... but business will lose its flexibility in order to make self servicing BI solutions...  15xx4n.gif

Link to comment
Share on other sites

4 minutes ago, loveindia said:

parsing antey analyzing the data, so JSON to sql result set techukoni we will analyze man... 15xx4n.gif

Asal Json em doing uncle analyze ante how? I see customer address testunav but did get what Ur doing??

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...