Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one string @data it is contained this value
@data=
{
	"dcrno": "20",
	"doctorcode": "di001",
	"salableitems": 
	[
	{
	"itemcode":"it0001"
	},
	{
	"itemcode":"it0003"
	},
	{
	"itemcode":"it0002"
	}
	],
	
	"sample/promo/gift": 
	[
	
	    {
	        "itemcode": "it001",
	        "itemqty": 10
	    },

	    {
	        "item code": "it002",
	        "itemqty": 2
	    },

	    {
	        "item code": "it009",
	        "itemqty": 7
	    }
	    
	],
	"pob": "788",
	"not": "rajvalue",
	"time": "13:78:90"
}


like
@data='@data=
{
	"dcrno": "20",
	"doctorcode": "di001",
	"salableitems": 
	[
	{
	"itemcode":"it0001"
	},
	{
	"itemcode":"it0003"
	},
	{
	"itemcode":"it0002"
	}
	],
	
	"sample/promo/gift": 
	[
	
	    {
	        "itemcode": "it001",
	        "itemqty": 10
	    },

	    {
	        "item code": "it002",
	        "itemqty": 2
	    },

	    {
	        "item code": "it009",
	        "itemqty": 7
	    }
	    
	],
	"pob": "788",
	"not": "rajvalue",
	"time": "13:78:90"
}'


now i want to convert into columns


like

dcrno,doctorcode,salable,pob,note,time
like this ..is it possible to convert likt this please help me to do that

What I have tried:

SQL
set @data=substring(@data,charindex(':',@data)),len(@data)-1)
Posted
Updated 5-Apr-16 11:59am
v4
Comments
dan!sh 2-Mar-16 0:44am    
Does not really make much sense. You do not post JSON to your database. What exactly are you looking for? Also, get rid of this massive JSON message just share the structure.

Do you HAVE to do this in SQL ? - it would be very messy - I guess you could write a stored procedure to do it

There's one glaring issue - saleable items - one {dcrno,doctorcode,salable,pob,note,time} 'record' for lack of a better word would have multiple salable items that seem to correspond to "sample/promo/gift" items - you you'd need to figure out how to handle them

One possible way of handling 'salable', encoding the item code and the quantity into a single string might look like

Field : Value
============
dcrno : 20
doctorcode : di001
salable : it001:10,it002:2,it003:7
pob : 788
note : rajvalue
time : 13:78:90

but then you need logic to encode and decode 'salable' - the 'obvious' way is to put salable items - the item code and the quantity into a separate table in the database, with a link back to the 'master' record

If I were doing it in any decent HLL, I'd parse the JSON to a (set of) POCO's and go from there - I'm not sure wether this tag 'sample/promo/gift' is strictly legal btw
 
Share this answer
 
Comments
Member 11337367 2-Mar-16 1:41am    
thanks for your kind reply on this ..can u be more specific on this it would be helpful more
Garth J Lancaster 2-Mar-16 5:02am    
assuming your JSON was legal, which it isnt "item<space>code" for example, I'd do something along the lines of


public class Salableitem
{
public string itemcode { get; set; }
}

public class SamplePromoGift
{
public string itemcode { get; set; }
public int itemqty { get; set; }
}

public class RootObject
{
public string dcrno { get; set; }
public string doctorcode { get; set; }
public List<salableitem> salableitems { get; set; }
public List<samplepromogift> samplepromogift { get; set; }
public string pob { get; set; }
public string not { get; set; }
public string time { get; set; }
}

RootObject jsonData = JsonConvert.DeserializeObject<rootobject>(data);

then maybe use linq to 'join' salableitems & samplepromogift
Member 11337367 2-Mar-16 5:44am    
i am getting syntax error..public function will work in sql?
Garth J Lancaster 2-Mar-16 5:48am    
for gods sake (whichever god you align with) - read what I said "in any decent HLL" - in this case, C# - if you want to do it in SQL server itself look at Solution 2 (although even he advises 'some CLR assembly that parses JSON' - do some research, dont expect it all to be handed to you 'on a plate'
In SQL Server 2016 you have functions that read values from JSON text (JSON_VALUE, OPENJSON, etc.) These will be available in Azure Sql Database soon.
If you have some older version of SQL Server, the best way is to use some CLR assembly that parses JSON such as JSONSelect.
 
Share this answer
 
Comments
Member 11337367 2-Mar-16 4:13am    
I have 2012 sql server any fuction is there in sql 2012.?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900