Forum Discussion
FOR JSON AUTO Query returning broken headers
As described in the following article, the returned JSON object is shown in SSMS as a single string, but in reality can be several rows.
In the result, the rows are separated by a newline.
https://learn.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server?view=sql-server-ver16&tabs=json-path#output-of-the-for-json-clause
I tested your described export path on your database, and when I opened the "result.json" file, I got a file with 33 rows of compacted JSON.
But since they were 33 rows, they were not _correct_ JSON, as the newlines separating these rows can happen at any point in the data.
Which means you need to concatenate your data before you format it out to readable JSON.
- Open your extracted file in VS Code
- Open the Find/Replace function
- Activate "RegEx" mode
- Find \n
- Replace with nothing
- Click Replace All
- Format
Newly opened Export with the find replace dialog open (ctrl + h)
After Replace
After Format (alt+shift+f)