简体   繁体   中英

Generate single complex JSON object from PostgreSQL query

I have a PostgreSQL database with the following tables:

Building

ID NAME DESCRIPTION
1 Building 1 1st Building Description
2 Building 2 2nd Building Description
3 Building 3 3rd Building Description

Floor

ID NAME DESCRIPTION BUILDING_ID
1 Floor 1.1 Floor Description 1.1 1
2 Floor 1.2 Floor Description 1.2 1
3 Floor 2.1 Floor Description 2.1 2
4 Floor 2.2 Floor Description 2.2 2
5 Floor 3.1 Floor Description 3.1 3

Room

ID NAME DESCRIPTION FLOOR_ID
1 Room 1.1.1 Room Description 1.1.1 1
2 Room 1.1.2 Room Description 1.1.2 1
3 Room 1.2.1 Room Description 1.2.1 2
4 Room 1.2.2 Room Description 1.2.2 2
5 Room 3.1.1 Room Description 2.1.1 5

The code for generating the above structure can be found in this SQL Fiddle , here .

I want to generate a single JSON object with a single query, with the following structure:

{
  "Building 1": {
    "details": {
      "id": 1,
      "name": "Building 1",
      "description": "1st Building Description"
    },
    "floors": {
      "Floor 1.1": {
        "details": {
          "id": 1,
          "name": "Floor 1.1",
          "description": "Floor Description 1.1"
        },
        "rooms": {
          "Room 1.1.1": {
            "id": 1,
            "name": "Room 1.1.1",
            "description": "Room Description 1.1.1"
          },
          "Room 1.1.2": {
            "id": 2,
            "name": "Room 1.1.2",
            "description": "Room Description 1.1.2"
          }
        }
      },
      "Floor 1.2": {
        "details": {
          "id": 2,
          "name": "Floor 1.2",
          "description": "Floor Description 1.2"
        },
        "rooms": {
          "Room 1.2.1": {
            "id": "3",
            "name": "Room 1.2.1",
            "description": "Room Description 1.2.1"
          },
          "Room 1.1.2": {
            "id": 4,
            "name": "Room 1.2.2",
            "description": "Room Description 1.2.2"
          }
        }
      },
    }
  },
  
  
  "Building 2": {
    "details": {
      "id": 2,
      "name": "Building 2",
      "description": "2nd Building Description"
    },
    "floors": {
      "Floor 2.1": {
        "details": {
          "id": 3,
          "name": "Floor 2.1",
          "description": "Floor Description 2.1"
        },
        "rooms": {
        }
      },
      "Floor 2.2": {
        "details": {
          "id": 4,
          "name": "Floor 2.2",
          "description": "Floor Description 2.2"
        },
        "rooms": {
        }
      },
    }
  },
  
    
  
  "Building 3": {
    "details": {
      "id": 3,
      "name": "Building 3",
      "description": "3rd Building Description"
    },
    "floors": {
      "Floor 3.1": {
        "details": {
          "id": 5,
          "name": "Floor 3.1",
          "description": "Floor Description 3.1"
        },
        "rooms": {
          "Room 3.1.1": {
            "id": 5,
            "name": "Room 3.1.1",
            "description": "Room Description 3.1.1"
          }
        }
      }
    }
  },
}

To explain the above structure:

  1. A single object output is needed
  2. The keys are always represented by the name of the objects
  3. All the fields are contained in a details object
  4. All the children objects are flattened inside another object in a similar way

Unfortunately, I can only use json_build_object because the above structure cannot be altered since it was already defined.

Thank you!

Her you can have an example of what you can do. You will have to correct and complete this script, but I think it can do the job

select json_build_object(b.name,json_build_object('details',row_to_json(b),
                        'floors',json_build_object('details',(select json_agg(row_to_json(f))->0 from (select * from floors f where f.building_id = b.id) f)))) 
from buildings b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM