This is my SQL Server table:
I have a table like this:
CREATE TABLE countries
(
continent nvarchar(10),
country nvarchar(10),
city nvarchar(10),
);
And I have this data like this:
INSERT INTO countries
VALUES ('asia', 'inda', 'new delhi'),
('asia', 'inda', 'hyderabad'),
('asia', 'inda', 'mumbai'),
('asia', 'korea', 'seoul'),
('asia', 'inda', 'milan'),
('europe', 'italy', 'rome'),
('europe', 'italy', 'milan');
I need JSON output in this format:
Asia
{
india
{
city: new delhi
city: Hyderabad
city: Mumbai
}
Korea
{
city: seoul
city: busan
}
}
Europe
{
Italy
{
city: rome
city:milan
city:naples
}
}
I tried so many queries, but I am not getting it:
select continent, country, city
from countries
group by continent, country
for json auto
which doesn't return the desired output.