In JSON, the structure is "Property" : "Value"
. You are attempting to create a JSON with the structure "Value" : "Value"
.
I'm not sure why you would want such a thing or even if it's a good idea, but to get to this you can create the output string yourself as gotqn suggested in his comment instead of using FOR JSON
.
First, create and populate sample table (Please save us this step in your future questions):
CREATE TABLE [user]
(
FirstName varchar(10),
LastName varchar(10)
)
INSERT INTO [User] (FirstName, LastName) VALUES
('Bruce', 'Wayne'),
('Clark', 'Kent'),
('Peter', 'Parker'),
('Tony', 'Stark')
The query:
SELECT STUFF(
(
SELECT ',{"'+ FirstName +'" : "'+ LastName +'"}'
FROM [User]
FOR XML PATH('')
)
, 1, 1, '[') + ']' As JsonOutput
Results:
JsonOutput
[{"Bruce" : "Wayne"},{"Clark" : "Kent"},{"Peter" : "Parker"},{"Tony" : "Stark"}]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…