-1
[
    {
        "intfName":"eth 1",
        "config":{
            "data":"value1"
        },
        "subIntf":[
            {
                "index": 1,
                "config": {
                    "key": "val11"
                }
            },
            {
                "index": 2,
                "config": {
                    "key": "val12"
                }
            }
        ]
    },
    {
        "intfName":"eth 2",
        "config":{
            "data":"value2"
        },
        "subIntf":[
            {
                "index": 1,
                "config": {
                    "key": "val21"
                }
            },
            {
                "index": 2,
                "config": {
                    "key": "val22"
                }
            }
        ]
    }
]

I have a JSON data (as shown above) in a json file. I need to populate this data into following two Postgres tables as shown below in the SELECT query results.
How can I achieve this?

CREATE TABLE interfaces 
(intfName varchar(25) PRIMARY KEY NOT NULL, data jsonb);

CREATE TABLE subinterfaces 
(index integer NOT NULL, intfName varchar(25) REFERENCES interfaces (intfName), data jsonb, PRIMARY KEY(index, intfName));


SELECT * FROM interfaces;
   intfName   |              data
--------------+---------------------------------
      eth 1   |  {"config": {"data":"value1"}}
      eth 2   |  {"config": {"data":"value2"}}



SELECT * FROM subinterfaces;
 index |  intfName |                 data                                     
-------+--------------+----------------------------------------
     1 |   eth 1   | {"index": 1, "config": { "key": "val11"}}
     2 |   eth 1   | {"index": 2, "config": { "key": "val12"}}
     1 |   eth 2   | {"index": 1, "config": { "key": "val21"}}
     2 |   eth 2   | {"index": 2, "config": { "key": "val22"}}
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.