4

I have a Json string. I Want to get value from that Json string.

This is my json string {"latitude":"22.5712854"},{"longitude":"88.4266847"}

I want only latitude and longitude from this, using TSQL query.

4
  • 3
    This could help you: simple-talk.com/sql/t-sql-programming/… Commented Jun 4, 2013 at 10:12
  • TSQL can't parse JSON natively and it's a very weak language for text parsing and handling in general (although you could parse the string you've shown using only SUBSTRING and CHARINDEX). It will almost certainly be easier to do this using code outside the database or possibly a CLR procedure; that way you can use a language that has JSON support or at least good text processing functions.
    – Pondlife
    Commented Jun 4, 2013 at 15:48
  • 2
    possible duplicate of Parse JSON in TSQL
    – AndrewC
    Commented Jun 4, 2013 at 23:40
  • Check the question duplicated here as flagged by AndrewC. Its answers are more thorough and it mentions the native JSON support in SQL Server 2016.
    – Frédéric
    Commented Feb 26, 2016 at 12:30

2 Answers 2

5

There is no native way to parse JSON in TSQL. But Phil Factor created his own implementation of paring JSON in SQL function. More about it on simple talk blog in article: Consuming JSON Strings in SQL Server

Aslo Ric Vander Ark created his own function but I did not tested it. You can read more on article: A Function to Split JSON Data

2
  • 2
    "There is no native way to parse JSON in TSQL. But Phil Factor created his own implementation [to parse JSON in TSQL]" <-- Contradiction at its best. Still, thanks for the link on how to parse JSON in TSQL. :)
    – rainabba
    Commented Oct 26, 2013 at 0:26
  • A custom implementation, as listed is different to a native (i.e. built in) way. Commented Jul 16, 2014 at 12:58
0

You could use JSON Select which has several functions for extracting different datatypes from JSON. For your requirements you could do something like this:

select 
    dbo.JsonDecimal(my_column, 'latitude') as latitude,
    dbo.JsonDecimal(my_column, 'longitude') as longitude
from my_table

DISCLOSURE: I am the author of JSON Select, and as such have an interest in you using it :)

1
  • Hi @joshuahealy, Could you please provide documentation of JsonSelect as the site is not online right now. Commented May 7 at 12:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.