I have mongodb document as an array and need to create json file in python.
mongodb document looks like
db.mappedfields.insertMany(
[
{ sourceAttribute: "first_name",
domainAttribute: "First_Name"},
{ sourceAttribute: "last_name",
domainAttribute: "Last_Name"}
]
Code tried
class create_dict(dict):
def __init__(self):
self = dict()
def add(self, key, value):
self[key] = value
mydict = create_dict()
i = 1
for key in mycol.find():
mydict.add(i, ({key['sourceAttribute']:key['domainAttribute']}))
i = i+1
json_data = json.dumps(mydict, indent=2, sort_keys=True)
Output getting
{
"1": {
"first_name": "First_Name"
},
"2": {
"last_name": "Last_Name"
}
}
Expected Output
{
"first_name": "First_Name",
"last_name": "Last_Name"
}
import json
import pymongo
class create_dict(dict):
# __init__ function
def __init__(self):
self = dict()
# Function to add key:value
def add(self, key, value):
self[key] = value
mydict = create_dict()
i = 1
for key in mycol.find():
mydict.add(i, ({key['input']:key['output']}))
i = i+1
json_data = json.dumps(mydict, indent=2, sort_keys=True)
json_data1 = json.loads(json_data)
dictlist = []
for key, value in json_data1.items():
dictlist.append(value)
print(dictlist)
result = {}
for d in dictlist:
result.update(d)
print(result)
Related
I have an object on dynamo db saved in the following way:
{
"name": "nameA",
...,
"properties": {
"prop1": "a",
...
}
}
If I pass the following object:
{
"name": "nameUpdate"
}
I would like to eventually get the following result:
{
"name": "nameUpdate",
...,
"properties": {
"prop1": "a",
...
}
}
The problem is that I get the object without the nested field:
{
"name": "nameUpdate",
...,
"properties": {}
}
MY APPROACH
To perform the update operation I am proceeding as follows:
def build_ddb_update_expression(data):
prefix = '#pf'
vals = {}
exp = 'SET '
attr_names = {}
for key, value in data.items():
vals[f':{key}'] = value
attr_names[f'#pf_{key}'] = key
exp += f'{prefix}_{key} = :{key}, '
exp = exp.rstrip(", ")
return vals, exp, attr_names
...
vals, exp, attr_names = build_ddb_update_expression(
json.loads(json.dumps(object_to_update), parse_float=decimal.Decimal))
response = table.update_item(
Key={'object_id': object_id},
ConditionExpression='attribute_exists(object_id)',
UpdateExpression=exp,
ExpressionAttributeValues=vals,
ExpressionAttributeNames=attr_names,
ReturnValues="ALL_NEW"
)
Has this ever happened to anyone?
Thanks in advance
Basically I am trying to make a code in python that takes a dictionary from a file. It should print the displayed keys.
{
"Name": "namename",
"Surname": "klsajdak",
"Mhtrwo": "lsdkaslkd",
"Phone": ["545454545454", "4554545454545"],
"Age": 84,
"kids": {
"Name": "Zero",
"Age": 0
}
}
my_dict = open("9listes.txt", "r")
for key,value in my_dict.items():
print("Key : {}".format(key))
You can achieve this via using json.load() as:
import json
with open('9listes.txt') as f:
my_dict = json.load(f) # `my_dict ` is the `dict` you need
# To print "key" & "value", uncomment below lines:
# for key, value in my_dict.items():
# print("Key: {}, Value: {}".format(key, value))
Refer json.load() document for more details.
I have a json object: users.json
{
"1" :
{ "name" : "Jason" } ,
"2" :
{ "name" : "Alex" }
}
I have a python function which takes as input a name and should return the "id". For example if I pass 'Jason', it should return '1' and if I pass 'Alex' it should return '2'. I know this is a simple question but I am really stuck...(and a bit lazy to study python dictionnaries...) Here is what I have so far
def __init__(self):
self.users_file = 'users.json'
def read_users_file(self):
with open(self.users_file) as users_file:
return json.load(users_file)
def get_user_id(self, name):
data = self.read_users_file()
values = data.values()
for val in data.values():
if(name == val.get('name')):
print('user found!')
Thanks!
data = {
"1":
{"name": "Jason"},
"2":
{"name": "Alex"}
}
name = 'Jason'
for key in d:
if (d[key]['name'] == name):
print(key) ## output 1
or in more Pythonic way:
for key, value in data.items():
if name == value['name']:
print(key)
I'm a bit new to Python Classes. I worked with python but not extensively with classes. So here is what I'm trying to do, is to read a JSON and convert the elements and nodes to a class and object, so I call functions to get values from the JSON.
{
"datamap": {
"version": "1.0",
"sourceProvider": "example_provider",
"logicalTables": [
{
"name": "region_table_one",
"physicalTable": "dbo_parent_user",
"logicalColumns": [
{
"name": "UID",
"physicalColumnName": "uid_number",
"displayName": "U Number",
"logicalDataType": "integer",
"inputType": {
"inputAction": "number",
"multiSelect": false
},
},
{
"name": "UID1",
"physicalColumnName": "uid_number1",
"displayName": "U Number1",
"logicalDataType": "integer",
"inputType": {
"inputAction": "number",
"multiSelect": false
},
},
]
},
{
"name": "region_table_two",
"physicalTable": "dbo_user_two",
"logicalColumns": [
{
"name": "UID2",
"physicalColumnName": "uid_number2",
"displayName": "U Number2",
"logicalDataType": "integer",
"inputType": {
"inputAction": "number",
"multiSelect": false
},
},
{
"name": "UID3",
"physicalColumnName": "uid_number3",
"displayName": "U Number3",
"logicalDataType": "integer",
"inputType": {
"inputAction": "number",
"multiSelect": false
},
},
]
}
]
}
}
The Python Class I wrote:
import json
class DataMap(object):
def __init__(self):
with open('datamap.json') as f:
self.__dict__ = json.load(f)
def get_logical_table(self, tableName):
if self.datamap['logicalTables']['name'] == tableName:
return datamap['logicalTables']['name']
obj = DataMap()
print(obj.datamap['logicalTables'])
#print(obj.get_logical_table('logicalTables'))
What I'm trying to do is if I call get_logical_table I should be able to get region_table_one and region_table_two.
is there any way that if I pass get_logical_table output to get the logicalColumns inside that JSON object.
I'm referencing:
- https://thepythonguru.com/reading-and-writing-json-in-python/
- Deserialize a json string to an object in python
To some extent, but stuck with reading notes to a class. Thanks for the help in advance.
Update:
import json
class DataMap(object):
def __init__(self):
self.logical_tables = None
with open('datamap.json') as f:
self.__dict__ = json.load(f)
self.data_map = self.__dict__['datamap']
def get_map_id(self):
return self.data_map['mapId']
def get_version(self):
return self.data_map['version']
def get_region(self):
return self.data_map['region']
def get_source_provider(self):
return self.data_map['sourceProvider']
def __getitem__(self, key):
return self.data_map[key]
def __repr__(self):
return repr(self.data_map)
def __len__(self):
return len(self.__dict__['datamap'])
def copy(self):
return self.data_map.copy()
def has_key(self, k):
return k in self.data_map
def keys(self):
return self.data_map.keys()
def values(self):
return self.data_map.values()
def items(self):
return self.data_map.items()
def pop(self, *args):
return self.data_map.pop(*args)
def __contains__(self, item):
return item in self.data_map
def __iter__(self):
return iter(self.data_map)
class LogicalTables(DataMap):
def __init__(self):
DataMap.__init__(self)
self.logical_tables = self.data_map['logicalTables']
logical_table = None
for table in self.get_all_logical_tables():
self.name = table.get("name")
print(self.name)
def __len__(self):
return len(self.data_map['logicalTables'])
def __repr__(self):
return repr(self.logical_tables)
def createName(self):
self.name = "Temporary Value"
def has_key(self, k, table_name=None):
"""Check if the dict has given key"""
logical_table = self.get_logical_table(table_name)
return k in logical_table
def get_all_logical_tables(self, tableName=None):
return self.data_map['logicalTables']
def get_logical_table(self, table_name=None):
logical_table = None
for table in self.get_all_logical_tables():
if table.get("name") == table_name:
logical_table = table
return logical_table
def get_logical_table_list(self, table_name=None):
table_list = []
for table in self.get_all_logical_tables():
table_list.append(table.get("name"))
return table_list
class LogicalColumns(LogicalTables):
def __init__(self):
LogicalTables.__init__(self)
self.logical_columns = self.logical_tables['logicalColumns']
def __len__(self):
return len(self.logical_columns['logicalColumns'])
def __repr__(self):
return repr(self.logical_columns)
I have updated and this is my current class.
logicalTables in your json input is actually a list, not a dict, so you doing ['logicalTables']['name'] won't work.
You need to change get_logical_table to something like this:
def get_logical_table(self, tableName):
for table in self.datamap['logicalTables']:
if table['name'] == tableName:
return table['logicalColumns']
Instead of iterating through lists, it would be better to transform your dict so you could directly access any logicalTable with its name (if they are unique).
Update:
You can transform the dict like so:
tables = {}
for table in self.datamap['logicalTables']:
name = table['name']
del table['name']
tables[name] = table
self.datamap['logicalTables'] = tables
# Use like so:
table_one = self.datamap['logicalTables']['region_table_one']
I am trying to simulate the problem statement using the below program:
import json
class System:
def __init__(self):
self.model = "abc"
self.fwVersion = "123"
self.prevfwVersion = "456"
self.safemodeVersion = "5756"
def __setitem__(self, key, val):
self.__dict__[key] = val
def __getitem__(self, key):
return self.__dict__[key]
def toJSON(self):
return self.__dict__
class Mainwall:
def __init__(self):
self.system = System()
def __setitem__(self, key, val):
self.__dict__[key] = val
def __getitem__(self, key):
return self.__dict__[key]
def toJSON(self):
return self.__dict__
class ComplexEncoder(json.JSONEncoder):
def default(self, obj):
if hasattr(obj, 'toJSON'):
return obj.toJSON()
else:
return json.JSONEncoder.default(self, obj)
fw = Mainwall()
def my_print():
print(json.dumps(fw.toJSON(), cls=ComplexEncoder, indent=4))
if __name__ == '__main__':
my_print()
Since python dictionary does not preserve the insertion order , the output of the above program is always will have the different key order.
Say, first time it prints:
{
"system": {
"safemodeVersion": "5756",
"prevfwVersion": "456",
"fwVersion": "123",
"model": "abc"
}
}
Second time it prints:
{
"system": {
"fwVersion": "123",
"prevfwVersion": "456",
"safemodeVersion": "5756",
"model": "abc"
}
}
But, in the output I would like to preserve the order in which the class members are initialized. i.e., Exactly as below:
{
"system": {
"model": "abc",
"fwVersion": "123",
"prevfwVersion": "456",
"safemodeVersion": "5756",
}
}
How to achieve the expected output for the same example using OrderedDict() or some other method?
There is a work-around I made for you. I invite you to look into the System() class. I created an OrderedDict() instead of four self attributes. Then, in you method toJSON(self):, instead of returning the self.__dict__ attributes, I am returning the OrderedDict() I set earlier.
class System:
def __init__(self, model='abc', fwVersion='123', prevfwVersion='456', safemodeVersion='5756'):
self.my_ordered_dict = OrderedDict()
self.my_ordered_dict['model'] = model
self.my_ordered_dict['fwVersion'] = fwVersion
self.my_ordered_dict['prevfwVersion'] = prevfwVersion
self.my_ordered_dict['safemodeVersion'] = safemodeVersion
# self.model = "abc"
# self.fwVersion = "123"
# self.prevfwVersion = "456"
# self.safemodeVersion = "5756"
def __setitem__(self, key, val):
self.__dict__[key] = val
def __getitem__(self, key):
return self.__dict__[key]
def toJSON(self):
return self.my_ordered_dict
This System() class instead of the one above, with the same code, outputs...
{
"system": {
"model": "abc",
"fwVersion": "123",
"prevfwVersion": "456",
"safemodeVersion": "5756"
}
}
upgrading to python 3.6 solved the problem.