162

What's the best SQL datatype for storing JSON string?

static List<ProductModel> CreateProductList()
{
    string json = @"[
        {
            ProductId: 1, 
            ProductCode: 'A', 
            Product: 'A'
        },
        {
            ProductId: 2, 
            ProductCode: 'B', 
            Product: 'B'
        }
    ]";

    IList<JToken> tokenList = JToken.Parse(json).ToList();
    List<ProductModel> productList = new List<ProductModel>();

    foreach (JToken token in tokenList)
    {
        productList.Add(JsonConvert.DeserializeObject<ProductModel>(token.ToString()));
    }

    return productList;
}

Which SQL datatype should we use for storing such a string containing JSON?

  • NVARCHAR(255)?
  • TEXT?
  • VARBINARY(MAX)?
2
  • 1
    Just some random noise (the comment, not the data): You might want to compress it too. In that case you need something binary. On the other hand: why not just design proper tables for the data?
    – The Nail
    Commented Feb 9, 2012 at 8:23
  • 3
    @The Nail: Sometimes storing something as JSON (or as a "document") is proper for the need. Like for a workflow engine or document management etc...I'm doing this on a current project, actually going from relational to document approach for the command side of my CQRS implementation. It's very fast if you use a serializer such as ServiceStack or JSON.Net.
    – swannee
    Commented May 30, 2012 at 21:46

5 Answers 5

229

Certainly NOT:

  • TEXT, NTEXT: those types are deprecated as of SQL Server 2005 and should not be used for new development. Use VARCHAR(MAX) or NVARCHAR(MAX) instead

  • IMAGE, VARBINARY(MAX) : IMAGE is deprecated just like TEXT/NTEXT, and there's really no point in storing a text string into a binary column....

So that basically leaves VARCHAR(x) or NVARCHAR(x): VARCHAR stores non-Unicode strings (1 byte per character) and NVARCHAR stores everything in a 2-byte-per-character Unicode mode. So do you need Unicode? Do you have Arabic, Hebrew, Chinese or other non-Western-European characters in your strings, potentially? Then go with NVARCHAR

The (N)VARCHAR columns come in two flavors: either you define a maximum length that results in 8000 bytes or less (VARCHAR up to 8000 characters, NVARCHAR up to 4000), or if that's not enough, use the (N)VARCHAR(MAX) versions, which store up to 2 GByte of data.

Update: SQL Server 2016 will have native JSON support - a new JSON datatype (which is based on nvarchar) will be introduced, as well as a FOR JSON command to convert output from a query into JSON format

Update #2: in the final product, Microsoft did not include a separate JSON datatype - instead, there are a number of JSON-functions (to package up database rows into JSON, or to parse JSON into relational data) which operate on columns of type NVARCHAR(n)

8
  • 32
    NVARCHAR should be the preferred choice as sql server 2016 will use it for its native JSON support blogs.msdn.com/b/jocapc/archive/2015/05/16/…
    – Loudenvier
    Commented Jul 23, 2015 at 17:08
  • @marc_s Is your "update" statement correct? I can't find any official JSON Data types... ?
    – Nix
    Commented Oct 12, 2016 at 19:58
  • 2
    @Nix: I think in the end, SQL Server supports JSON functions that operate on NVARCHAR(n) data types
    – marc_s
    Commented Oct 12, 2016 at 20:24
  • 2
    You might want to update your answer to not state there is a Json data type
    – Nix
    Commented Oct 12, 2016 at 21:42
  • 2
    varbinary(max) could be used when using compression Commented Oct 17, 2017 at 13:30
34

I shall go for nvarchar(max). That should fit the requirement.

Update: With SQL Server 2016 and Azure SQL, there are a lot of additional native JSON capabilities. This might positively impact your design or approach. You may read this for more: https://learn.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server

7
  • 8
    Do you really need the 2-byte-per-character Unicode storage?? Depending on your data - it might just be wasting twice as much bytes as needed... (but if you DO need Unicode - then that's the only way to go, I agree!)
    – marc_s
    Commented Feb 9, 2012 at 8:47
  • 5
    nvarchar - because the data is not defined. If we feel that the system will not need unicode, we can save moving to varchar(max)
    – Kangkan
    Commented Oct 19, 2012 at 5:04
  • 5
    Also, using nvarchar avoids the collation issues you will eventually have when using varchar, but it will be slower in query performance than varchar. Great DBA question with further info.
    – Scotty.NET
    Commented Sep 11, 2013 at 9:13
  • 5
    How did this question get so many upvotes? So it says which data type to use, fine... but it doesn't even try to explain why that would be the right choice. Commented Mar 16, 2015 at 22:41
  • 1
    You can always use varchar and escape any unicode chars. This is a good approach if you will only have occasional unicode chars in your text as it saves space over using a nvarchar
    – chrisb
    Commented Nov 24, 2015 at 16:21
4

Recommended data type is NVARCHAR.
Please refer https://learn.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-ver15#store-and-index-json-data-in-sql-server][1]

1
  • The page you link to doesn't make any such recommendation. It just says "JSON text is stored in VARCHAR or NVARCHAR columns and is indexed as plain text. " - it uses NVARCHAR in all the examples which is understandable as it has no idea what characters people will be using this code with Commented Dec 21, 2023 at 10:15
3

I would recommend to use nvarchar(max) if you plan to use JSON features on SQL 2016 or Azure SQL.

If you don't plan to use those features, you could use varbinary(max) combined with COMPRESS (and DECOMPRESS) functions. More information: https://blogs.msdn.microsoft.com/sqlserverstorageengine/2015/11/23/storing-json-in-sql-server/

COMPRESS and DECOMPRESS functions use standard GZip compression. If your client can handle GZip compression (e.g browser that understands gzip content), you can directly return compressed content. Note that this is performance/storage trade-off. If you frequently query compressed data you mig have slower performance because text must be decompressed each time.

1
  • which are JSON features on SQL 2016 ?
    – Kiquenet
    Commented Mar 21, 2018 at 10:14
1

It's recommend to use varchar/nvarchar datatype then with the JSON text that's stored in database tables, you can read or modify values in the JSON text by using the following built-in functions:

  • ISJSON (Transact-SQL) tests whether a string contains valid JSON.
  • JSON_VALUE (Transact-SQL) extracts a scalar value from a JSON string.
  • JSON_QUERY (Transact-SQL) extracts an object or an array from a JSON string.
  • JSON_MODIFY (Transact-SQL) changes a value in a JSON string.

Please refer this SQL Server documentation about JSON data in SQL Server

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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