0

I have a nested Python dictionary which looks as so

master_config_skeleton = {
"ingestion_config":
    {
    "location":{}, 
    "start_sequence":{}, 
    "datafeed":
        {
        "t04047":
            {
            "validation":
                {
                "triple_check":{},
                "record_count_validation":{}
                }, 
            "date_pattern":{}, 
            "cdc_config": {}
            }
        }
    }
}

I also have a data frame that I have converted to a dictionary as below.

[{'source': 'FLEXCAB', 'app': 'Replicator', 'feed_id': 2382, 'seq_type': 'SEQUENCE', 'hdfs_home_dir': '/data/b2b'}]

I am looking to append this dictionary to the nested Python dictionary master_config_skeleton to transform it as below:-

master_config_skeleton = {
"ingestion_config":
    {
    "source": "FLEXCAB",
    "app": "Replicator",
    "feed_id": "2382",
    "seq_type": "SEQUENCE",
    "hdfs_home_dir": "/data/b2b",
    "location":{}, 
    "start_sequence":{}, 
    "datafeed":
        {
        "t04047":
            {
            "validation":
                {
                "triple_check":{},
                "record_count_validation":{}
                }, 
            "date_pattern":{}, 
            "cdc_config": {}
            }
        }
    }
}
Balajee Addanki
  • 690
  • 2
  • 9
  • 23

2 Answers2

1

Create dictionary from first value of list and merge together:

L = [{'source': 'FLEXCAB', 'app': 'Replicator', 'feed_id': 2382, 
      'seq_type': 'SEQUENCE', 'hdfs_home_dir': '/data/b2b'}]

#https://stackoverflow.com/a/7205107    
out = merge(master_config_skeleton, {'ingestion_config':L[0]})
print (out)

{
  "ingestion_config": {
    "location": {},
    "start_sequence": {},
    "datafeed": {
      "t04047": {
        "validation": {
          "triple_check": {},
          "record_count_validation": {}
        },
        "date_pattern": {},
        "cdc_config": {}
      }
    },
    "source": "FLEXCAB",
    "app": "Replicator",
    "feed_id": 2382,
    "seq_type": "SEQUENCE",
    "hdfs_home_dir": "/data/b2b"
  }
}
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

You can use the update method of dict, It takes all the keys from another dictionary and add it to the original dictionary, Pleas notice that this method overrides existing keys

For example this code

d1 = {'A': 'Alice', 'B': 'Bob'}
d2 = {'B': 'Barak', 'C': 'Carl'}
d1.update(d2)
print(d1)

Would print this output

{'A': 'Alice', 'B': 'Barak', 'C': 'Carl'}