You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some database (such as SQL Server) allow you to run queries that reason about JSON data in a column. It would be good to enable query to be able to make use of this, i.e. be able to sort/filter/etc. by values that part of a JSON column.
When looking at this we should also consider enabling taking that JSON data from a single column and deserializing it into multiple properties.
Original Contents
Adding some more general comments for the scenario this is tracking, but keeping the original contents of the issue
Reacted by Michael A. Volz (Flynn), Dennis Roche, Marian Zagoruiko, Coskun Sunali, Sergey Pismennyi, Dylan, excelsior, Shimmy Weitzhandler, Khalil Mohammad Mirza, Alexander Trauzzi and 3 more
Hoping that there is some ability to query against the json document similar to how postgresql can do already today. Even better is if the ability to query can be done from EF through Linq.
We have been discussing this with the SQL Server team. I'm just waiting to hear back from them about how much detail of the feature I can discuss publically before answering this one 😄
So JSON support is mostly about the FOR JSON key words which allow you to render a result set as JSON.
The issue is that when using this with EF the expectation would be that the JSON would be shaped after your domain model rather than the database schema. For example, if the [dbo].[products].[product_id] column is mapped to the Product.ProductId property then EF users would expect the JSON to use Product/ProductId rather than products/product_id. Of course for this simple scenario we could just use column aliasing in the query, however it gets more complicated when you start having relationships, tables split to multiple entity classes, tables combined into a single entity class, inheritance, etc. Bottom line is that I’m not sure how feasible it would be to actually use the FROM JSON syntax since the logic to do all this mapping etc. is handled by processing the query results.
Wow, so no extensions to TSQL so we can query the data? That would be very disappointing. Would you be able to to point us to a doc/spec that describes JSON support in SQL 2016?
It seems se are talking about two separare things:
The ability to return query resulta directly formatted as JSON from the server, which is what I know the upcoming 'FOR JSON' feature does and what Rowan mentions we have been discussing with the SQL Server team
The ability to query inside JSON documents stored in SQL Server directly in SQL, which I don't know if it is coming but that I will ask about.
Reacted by Michael A. Volz (Flynn), max-favilli, Makla and Ryouko Konpaku
Regarding the ability to return results as JSON in particular, another potential mismatch between the SQL Server feature and EF7 is the fact that the latter does not guarantee that a LINQ query will be translated into a single SQL query, e.g. currently if the 'Include()' method is used with collection navigation properties, EF7 can translate into multiple SQL queries. An hypothetical 'ForJson()' extension method would then need to concatenate multiple results in a way that made sense.
In SQL Server we will represent JSON as plain string. I don't believe that native JSON type will be added in 2016 version. However, we will have built-in functions that find values in JSON string using some JavaScript-like path. As an example, in PostgreSQL you can use #>> operator to find a value on the path "a[2]":
I'm not sure how you would like to query JSON via EF. One option might be to represent some string column that contains JSON (e.g. jsonProperty) as a dynamic property in EF and reference some "virtual field" in that dynamic property (e.g. entity.jsonProperty.info.address[2].town). In this case maybe we might transform this expression to something like:
@JocaPC thanks for your input. I would expect to have a single column in the database with the entity fully serialized there. I can imagine we could have some additional columns like primary key. When it comes to queries I would like to have implemented as many LINQ methods as possible.
Re SQL 2016 if it simply is a plain string then I can imagine there will be no indexes so all queries will be doing table scan ?
Postgresql queries against jsonb structures are fully indexed. Wonder why that isn't possible with SQL Server as well. I can see this neutered behavior to exist as a stepping stone, but as is it will have little value to me.
Since JSON support in SQL Server has different functionalities we should be more specific and describe what could be integrated. Probably FOR JSON does not makes sense for EF integration, but some functions might be useful. I have proposed one change in #4021 so we can start discussion about this use case there.
If you have another ideas how EF/SQL Server integration might look you can create additional issues and describe scenario.
@JocaPC sounds good. I think #4021 represent a good set of goals for JSON support in EF. I would prefer to close this issue (#2141) and use that one instead to track the feature in our backlog.
Activity
CoreyKaylor commentedon May 9, 2015
Hoping that there is some ability to query against the json document similar to how postgresql can do already today. Even better is if the ability to query can be done from EF through Linq.
rowanmiller commentedon May 12, 2015
We have been discussing this with the SQL Server team. I'm just waiting to hear back from them about how much detail of the feature I can discuss publically before answering this one 😄
rowanmiller commentedon May 12, 2015
So JSON support is mostly about the
FOR JSONkey words which allow you to render a result set as JSON.The issue is that when using this with EF the expectation would be that the JSON would be shaped after your domain model rather than the database schema. For example, if the [dbo].[products].[product_id] column is mapped to the Product.ProductId property then EF users would expect the JSON to use Product/ProductId rather than products/product_id. Of course for this simple scenario we could just use column aliasing in the query, however it gets more complicated when you start having relationships, tables split to multiple entity classes, tables combined into a single entity class, inheritance, etc. Bottom line is that I’m not sure how feasible it would be to actually use the FROM JSON syntax since the logic to do all this mapping etc. is handled by processing the query results.
pawelpabich commentedon May 12, 2015
Wow, so no extensions to TSQL so we can query the data? That would be very disappointing. Would you be able to to point us to a doc/spec that describes JSON support in SQL 2016?
ErikEJ commentedon May 13, 2015
@pawelpabich Be careful what you ask for - http://www.brentozar.com/archive/2015/05/reading-the-sql-server-2016-data-sheet/
pawelpabich commentedon May 13, 2015
@ErikEJ PostgreSQL has done it and it seems to work well. I trust Microsoft engineers can do at least as good job.
divega commentedon May 13, 2015
It seems se are talking about two separare things:
divega commentedon May 13, 2015
Regarding the ability to return results as JSON in particular, another potential mismatch between the SQL Server feature and EF7 is the fact that the latter does not guarantee that a LINQ query will be translated into a single SQL query, e.g. currently if the 'Include()' method is used with collection navigation properties, EF7 can translate into multiple SQL queries. An hypothetical 'ForJson()' extension method would then need to concatenate multiple results in a way that made sense.
JocaPC commentedon May 13, 2015
In SQL Server we will represent JSON as plain string. I don't believe that native JSON type will be added in 2016 version. However, we will have built-in functions that find values in JSON string using some JavaScript-like path. As an example, in PostgreSQL you can use #>> operator to find a value on the path "a[2]":
SELECT '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'
We will have JSON_VALUE built-in function instead of #>> operator and more JavaScript friendly path syntax:
SELECT JSON_VALUE('{"a":[1,2,3],"b":[4,5,6]}', '$.a[2]')
I'm not sure how you would like to query JSON via EF. One option might be to represent some string column that contains JSON (e.g. jsonProperty) as a dynamic property in EF and reference some "virtual field" in that dynamic property (e.g. entity.jsonProperty.info.address[2].town). In this case maybe we might transform this expression to something like:
JSON_VALUE(entity.jsonProperty, '$.info.address[2].town')
Is this something that you will like to see in EF?
pawelpabich commentedon May 13, 2015
@JocaPC thanks for your input. I would expect to have a single column in the database with the entity fully serialized there. I can imagine we could have some additional columns like primary key. When it comes to queries I would like to have implemented as many LINQ methods as possible.
Re SQL 2016 if it simply is a plain string then I can imagine there will be no indexes so all queries will be doing table scan ?
CoreyKaylor commentedon May 13, 2015
Postgresql queries against jsonb structures are fully indexed. Wonder why that isn't possible with SQL Server as well. I can see this neutered behavior to exist as a stepping stone, but as is it will have little value to me.
13 remaining items
JocaPC commentedon Dec 9, 2015
Since JSON support in SQL Server has different functionalities we should be more specific and describe what could be integrated. Probably FOR JSON does not makes sense for EF integration, but some functions might be useful. I have proposed one change in #4021 so we can start discussion about this use case there.
If you have another ideas how EF/SQL Server integration might look you can create additional issues and describe scenario.
divega commentedon Dec 9, 2015
@JocaPC sounds good. I think #4021 represent a good set of goals for JSON support in EF. I would prefer to close this issue (#2141) and use that one instead to track the feature in our backlog.
cc @rowanmiller
rowanmiller commentedon Dec 9, 2015
Agreed. #4021 covers everything from we intended in this issue, and more.