Jump to content

SQL Server 2016 Json parsing super man...


loveindia

Recommended Posts

  • Replies 43
  • Created
  • Last Reply

Top Posters In This Topic

  • loveindia

    17

  • k2s

    3

  • Kontekurradu

    3

  • yomama

    3

Top Posters In This Topic

1 hour ago, loveindia said:

so just imagine, you have a webpage where you have the option to update your address or a api call that will send JSON data to add a new customer and so on... earlier since Json is not available in SQL Server, you have to either convert it to XML and then do via SQL or insert by means of application code only... so enta complex logic untey application code anta complex avutundey and there is no more concept of batch processing... and JSON is highly in demand now... so Microsoft started getting that into T-SQL straight... now just JSON pass chestey chalu I can interpret the data myself and work with it and it will faster than the application level of using it man....  15xx4n.gif

Light ga ardam ayindi uncle hands on chesta clarity vastadi 

Link to comment
Share on other sites

2 hours ago, loveindia said:

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

its not about easy to understand...most easy to understand/readable is xml for both human and computer..json is less weight thats the biggest bet we get..less weight on wire

Link to comment
Share on other sites

30 minutes ago, rikki said:

its not about easy to understand...most easy to understand is xml both human and computer..json is less weight thats the biggest bet we get..less weight on wire

ya but xml creation is pain without nodes... from application side easy emo kaani db side nunchi aitey every node match ayyelaaga cheyyali, so much work man...

Link to comment
Share on other sites

3 hours ago, loveindia said:

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
	

 

Interesting.

 

Whats the need to send JSON to SQLServer man. This scenario looks helpful, if client gets response as JSON object and he wants to save it immediately.

But generally apart of storing data, if we need to do loads of business logic, for that its better to use Object inplace of JSON. and there are simple conversion available JSON to Object by using GSON jar.

 

 

Link to comment
Share on other sites

2 hours ago, loveindia said:

that means you can query the JSON directly and treat it as data directly... earlier you have to read json and then parse it to an intermediate table(which is dead slow) and then do the operations... now since this is all inbuilt, you can start directly treating the JSON as final version of data and do what ever you like to do with that data man...  15xx4n.gif

But JAVA- Hiberante use chesthey its straightforward, JSON gets convert to domain object that store in DB.

 

 

Link to comment
Share on other sites

11 minutes ago, gunturodu1 said:

But JAVA- Hiberante use chesthey its straightforward, JSON gets convert to domain object that store in DB.

 

 

I am not Java pro man, just basic beginner level...

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...