{"id":14069835,"url":"https://github.com/Phil-Factor/JSONSQLServerRoutines","last_synced_at":"2025-07-30T06:33:23.305Z","repository":{"id":147519965,"uuid":"155745596","full_name":"Phil-Factor/JSONSQLServerRoutines","owner":"Phil-Factor","description":"Various stored procedures that are used to import and export JSON Data","archived":false,"fork":false,"pushed_at":"2020-11-03T15:21:11.000Z","size":22684,"stargazers_count":44,"open_issues_count":1,"forks_count":16,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-13T07:16:21.005Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Phil-Factor.png","metadata":{"files":{"readme":"ReadMe.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-11-01T16:42:23.000Z","updated_at":"2024-05-16T23:46:41.000Z","dependencies_parsed_at":"2024-02-29T08:34:08.635Z","dependency_job_id":null,"html_url":"https://github.com/Phil-Factor/JSONSQLServerRoutines","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Phil-Factor%2FJSONSQLServerRoutines","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Phil-Factor%2FJSONSQLServerRoutines/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Phil-Factor%2FJSONSQLServerRoutines/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Phil-Factor%2FJSONSQLServerRoutines/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Phil-Factor","download_url":"https://codeload.github.com/Phil-Factor/JSONSQLServerRoutines/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228102351,"owners_count":17869834,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-13T07:07:16.812Z","updated_at":"2024-12-04T11:31:05.597Z","avatar_url":"https://github.com/Phil-Factor.png","language":"TSQL","readme":"# SQL Server and JSON: Data import, export and validation\n\nThis repository contains several stored procedures and PowerShell routines that can be used for inserting JSON data into SQL Server, validate the data  or to export the data from SQL Server. They are described in a series of articles on the Red-Gate site and in Simple-Talk. Some use ordinary JSON files and others show how to include the schema and data in one document. I use\nthem as temporary procedures because these are better for cross-database work, and as I\ngenerally script them there is little point in maintaining a permanent procedure on every server.\n\n## Schema creation\n\n### CreateJSONSchemaFromTable.sql\n\nThis temporary procedure creates a JSON schema from a table that\nmatches the JSON you will get from doing a\nclassic `select * from ... FOR JSON` statement on the entire table. This procedure needs SQL Server 2017 or later since it relies on `string_agg()`.\n\n### CreateJSONArrayInArraySchemaFromTable.sql\n\nThis will produce a JSON Array-in-Array schema from a table.\nThis procedure needs SQL Server 2017 or later since it relies on `string_agg()`.\n\n## JSON Data creation\n\n###  SaveJsonDataFromTable.sql\n\nThis gets the JSON data from a table, taking into account all the problems that can come about such as dealing with CLR data types\n\n### SaveExtendedJsonDataFromTable.sql\n\nThis temporary procedure This gets the JSON data from a table, taking into account all the problems that can come about such as dealing with CLR data types\n\n### ArrayInArrayJSONDataFromTable.sql\n\nThis will produce a JSON Array-in-Array schema from either a table or a query.\nThis can't be done, unfortunately, from SQL Server's implementation of JSON.\nThis procedure needs SQL Server 2017 or later since it relies on `string_agg()`. It was\nfirst written to check out how much more economical on space the array-in-Array\nformat was for storing tabular data as a document\n\n## Importing JSON Data\n\n### SaveJSONDataToTable.sql\n\nThis saves a JSON file to a table, dealing with the complications of having old deprecated data types and CLR data types\n\n###  SelectJSONIntoTable.sql\n\nThis is a way of taking a JSON string that contains both the schema\nand the data, and inserting it into a table in the database you wish\n\n## Utilities\n\n###  SaveJSONToFile.sql\n\nThis is a utility stored procedure for\nsaving text to a file It is designed to save\nas utf-8 for JSON files but will do any file\n\n###  SaveMultiRowSelectStatementFromTable.sql \n\nThis gets a multirow derived table SELECT * from (VALUES)  statement \nfrom a table or a query. If you provide a destination, it will create\nthe entire statement to stock the table with data. Warning: This is slow to use with large tables- use JSON instead as it is quicker, surprisingly.\n  \n###  SaveMergeStatementFromTable \n\nThis creates a merge statement, creating a table source from a multi-row \nVALUES statement, and merging it with the table whose name you provide.\nThis MERGE statement can then be executed. Beware\nthat this is only really practicable for small tables, because the VALUES statement degrades with scale.\n\nThe source is specified either by the database.schema.table 'tablespec, \nor by doing tablename, schema and database individually. You can also use queries\n  \n## JSON Data Discovery\n\n###  UnwrapJSON.sql\n\nThis multi-statement table-valued function talkes a JSON string and\n  unwraps it into a relational hierarchy table that also retains\n  the path to each element in the JSON document, and calculates the\n  best-fit sql datatype for every simple value\n\n###  OpenJSONExpressions.sql \n\n  This inline table-valued function talkes a JSON string and\n  locates every table structure. Then it creates an OpenJSON\n  Statement that can then be executed to create that table\n  from the original JSON.\n  \n###  TablesFromJSON \n\n This procedure returns a table for every one found  in a JSON \n  string \n   \n  \n## PowerShell routines \n\n### ValidateViaJSONSchema.ps1\n\nShows how to validate a directory, with a subdirectory with the schema and a parallel directory with the data\n\n### GenerateSQLMergeScripts.ps1\n\nThis is a PowerShell script uses SaveMergeStatementFromTable.sql to generate a gigantic merge script for all the tables of a (small) database.\n","funding_links":[],"categories":["TSQL"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPhil-Factor%2FJSONSQLServerRoutines","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPhil-Factor%2FJSONSQLServerRoutines","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPhil-Factor%2FJSONSQLServerRoutines/lists"}