JSON from SQL query with child level

Danilo Körber

I have a simple query in my database:

SELECT id, name FROM users FOR JSON AUTO, ROOT('users')

This returns the following JSON:

{
    "users": [
        {"id": "1", "name": "John"}
        {"id": "2", "name": "Mike"}
    ]
}

I want to have the return with the following format:

{
    "users": {
        "list": [
            {"id": "1", "name": "John"}
            {"id": "2", "name": "Mike"}
        ]
    }
}

Can I do this on SQL level by simply changing the query?

Zhorov

You may try with this:

Table:

CREATE TABLE users (
   id varchar(1),
   [name] varchar(50)
)
INSERT INTO users
   (id, [name])
VALUES
   ('1', 'John'),
   ('2', 'Mike')

Statement:

SELECT users = (SELECT id, name FROM users FOR JSON AUTO, ROOT('list'))
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER

Result:

{"users":{"list":[{"id":"1","name":"John"},{"id":"2","name":"Mike"}]}}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Entity Framework Core Parent/Child Clean query with multi level child

分類Dev

select query for json in sql

分類Dev

No results from SQL Query

分類Dev

No results from SQL Query

分類Dev

Substract query from another query in SQL Server

分類Dev

Convert a query from SQL to CodeIgniter

分類Dev

Recursive Parent/Child in same table query in SQL where parent is PK

分類Dev

How to get all child of a given id in SQL Server query

分類Dev

Looping result from SQL select query

分類Dev

SQL Query for max value from subsets

分類Dev

Decode text from a FOR XML Sql Query

分類Dev

Query from multiple XML columns in SQL Server

分類Dev

How to make a dynatree from sql query results?

分類Dev

SQL query: combining data from two tables

分類Dev

Sql query selecting from both tables.

分類Dev

Output complex xml from SQL select query

分類Dev

JasperReports. Using value from parent json in child table

分類Dev

SQL Query - Query on Current Date but condition from the past

分類Dev

SQL Query between two dates from sub query

分類Dev

sql query to select a random row from a table based on the previous query

分類Dev

Derived query throws error ORA-00604: error occurred at recursive SQL level 1

分類Dev

How to query specific column name from multiple table from SQL

分類Dev

Postgres SQL Query that returns JSON Object with IDs as dynamic keys

分類Dev

How to modify a JSON to a particular SQL query format in javascript?

分類Dev

How to create a multi-level json file and then read specific values from python?

分類Dev

Set Transaction Isolation Level Disappears From table-valued function T-SQL In Management Studio

分類Dev

Get a list of JSON property names from a class to use in a query string

分類Dev

How to query deep nested json array from couchbase?

分類Dev

Subtract hours from SQL Server 2012 query result

Related 関連記事

  1. 1

    Entity Framework Core Parent/Child Clean query with multi level child

  2. 2

    select query for json in sql

  3. 3

    No results from SQL Query

  4. 4

    No results from SQL Query

  5. 5

    Substract query from another query in SQL Server

  6. 6

    Convert a query from SQL to CodeIgniter

  7. 7

    Recursive Parent/Child in same table query in SQL where parent is PK

  8. 8

    How to get all child of a given id in SQL Server query

  9. 9

    Looping result from SQL select query

  10. 10

    SQL Query for max value from subsets

  11. 11

    Decode text from a FOR XML Sql Query

  12. 12

    Query from multiple XML columns in SQL Server

  13. 13

    How to make a dynatree from sql query results?

  14. 14

    SQL query: combining data from two tables

  15. 15

    Sql query selecting from both tables.

  16. 16

    Output complex xml from SQL select query

  17. 17

    JasperReports. Using value from parent json in child table

  18. 18

    SQL Query - Query on Current Date but condition from the past

  19. 19

    SQL Query between two dates from sub query

  20. 20

    sql query to select a random row from a table based on the previous query

  21. 21

    Derived query throws error ORA-00604: error occurred at recursive SQL level 1

  22. 22

    How to query specific column name from multiple table from SQL

  23. 23

    Postgres SQL Query that returns JSON Object with IDs as dynamic keys

  24. 24

    How to modify a JSON to a particular SQL query format in javascript?

  25. 25

    How to create a multi-level json file and then read specific values from python?

  26. 26

    Set Transaction Isolation Level Disappears From table-valued function T-SQL In Management Studio

  27. 27

    Get a list of JSON property names from a class to use in a query string

  28. 28

    How to query deep nested json array from couchbase?

  29. 29

    Subtract hours from SQL Server 2012 query result

ホットタグ

アーカイブ