I posted earlier today this question related to the conversion of a JSON array into a table, and was quite lucky to find the solution after further search.
Now, and after searching more than the previous time, I'm still stuck (though I saw some entries in this forum, but they do not specifically resolve my problem).
There are some cases in which I need to respond to a request with the result of a select that has a variable number of records (could be thousands), each having about 20 columns.
Now the way I found to build a JSON out of the select (by adding FOR JSON AUTO
) works very nicely and indeed creates an array of records, each having all the columns presided by the column name.
This, however, makes the result several times larger than needed (I'm thinking about network traffic, specially when it is not over a LAN).
To overcome this, I split the response into two, a Header and a Body, where the Header contains the list of the column names in the response (in the correct order) while the body contains, for each record, the list of values (matching the number and order of the Header).
Example:
If the source table would look like this:
A | B | C
--------+-------------+--------------------
11 | 22 | 2018-04-07 12:44
33 | 44 | 2017-02-21 18:55
55 | 66 | 2016-11-12 00:03
and the Body of the response should contain the values of columns "A" and "B" from a table, the response would look as follows:
{"Response": {
"Header":["A","B","C"],
"Body":[["11","22","2018-04-07 12:44"],
["33","44","2017-02-21 18:55"],
["55","66","2016-11-12 00:03"]
]
}
}
Unfortunately, I'm not finding a way to get the contents of the Body
without the "A"
,"B"
and "C"
names.
Update
I want to stress the fact that the different columns within the table record may be of different types, so I would convert them all to strings. See the updated example table and the expected result.