Skip to content
Advertisement

Migrate columns to JSON in SQL

I have a table (Table A) that looks like this:

I want to convert it to a table (Table B) like this

How do you insert/transform into a json column? Or do I just have to construct the json string myself?

Additionally, does the nullableness of the int columns affect creation? Are clicks for example not included or do they do something like this {"clicks": null }?

I read through Microsoft’s documentation about JSON, but I didn’t see any reference to inserting from SQL columns. Only from other JSON data.

Advertisement

Answer

You simply need FOR JSON PATH with WITHOUT_ARRAY_WRAPPER and INCLUDE_NULL_VALUES options:

Table:

Statement:

Result:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement