HeyJude Asks: How to construct a JSON_QUERY that converts a TOP 1 record into a json object (instead of a json table)
I want to use a
I want to use a
SELECT TOP 1query such that the resultant json has that top 1 record in object form, instead of in table form?
For example, the following query (live demo):
CREATE TABLE Trees ( [Id] INT, [Type] NVARCHAR(100), [Height] DECIMAL(2,1) ); INSERT INTO Trees ([Id], [Type], [Height]) VALUES (1, 'Palm', 5.5), (2, 'Pine', 6.2), (3, 'Apple', 2.5), (4, 'Japanese Cedar', 0.5), (5, 'Spanish Fir', 0.6); SELECT highestTree = JSON_QUERY( ( SELECT TOP 1 Id as id, Type as type, Height as height FROM Trees WHERE Height = (SELECT Max(Height) FROM Trees) FOR JSON PATH ) ), lowestTree = JSON_QUERY( ( SELECT TOP 1 Id as id, Type as type, Height as height FROM Trees WHERE Height = (SELECT MIN(Height) FROM Trees) FOR JSON PATH ) ) FOR JSON PATH, WITHOUT_ARRAY_WRAPPER ;
But I want:
Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.