what is the Proper way to insert json into postgresql table?

25 views Asked by At

My json file looks something like this:

[
  {
    "id": 1,
    "date": "2023-02-09",
    "homeTeam": {
      "id": "3",
      "players": [
        {
          "id": "5",
          "isInjured": "no",
          "shots": [
            { "Goal": "yes", "celebrated" : true},
            { "Goal": "yes", "celebrated" : true}
          ]
        }
      ]
    }
  }
]

I used python to load the data into psql database. How do i structure my table to insert data from python? I am not writting the data manualy, all the data is already in a json file.

How exactly do i insert into table using select?

My table currently looks like this:

CREATE TABLE IF NOT EXISTS games(
  id INT NOT NULL PRIMARY KEY
  , date DATE NOT NULL
  , homeTeam json
  , awayTeam json
);

I tried json_populate_recordset(NULL::games, %s); but it only inserts values to the first id and date.

0

There are 0 answers