Save a Python dictionary in JSON format - json

I have some data in the following variables,
want to store the python dictionary in the following JSON format,
{
'name': {
'Address': '123 abc, IN, 50012',
'Phone': '123456',
},
'name2': {
'Address': '123 abc, IN, 50012',
'Phone': '123456',
},
}
Updated JSON format(Required in this format)
{
"name":[
{"Address": "123 abc, IN, 50012"},
{"Phone": "123456"}
],
"name2": [
{"Address": "123 abc, IN, 50012"},
{"Phone": "123456"}
]
}
But I managed only to get the JSON in this format,
{"phone": "123456738", "address": "address1,", "name": "name1"}
{"phone": "123456178", "address": "address2,", "name": "name2"}
{"phone": "123452678", "address": "address3,", "name": "name3"}
below is my code
#getting data and adding to dict
dict_list = []
for data in all_data:
dict = {}
dict["phone"] = phone.strip()
dict["address"] = address.strip()
dict["name"] = name.strip()
dict_list.append(dict)
#writing to json file
with open('data.json', 'w') as outfile:
for details in dict_list:
r = json.dumps(details)
loaded_r = json.loads(r)
json.dump(loaded_r, outfile)

you want to convert the inner dictionary to a list of dictionaries containing only one key & value. Doable with some comprehension syntax:
in_dict = {
'name': {
'Address': '123 abc, IN, 50012',
'Phone': '123456',
},
'name2': {
'Address': '123 abc, IN, 50012',
'Phone': '123456',
},
}
out_dict = {k : [{k2:v2} for k2,v2 in v.items()] for k,v in in_dict.items()}
For each key,value (dictionary) pair, iterate on items of the sub-dict and create 1 dictionary per key,value in a list.
result:
>>> out_dict
{'name': [{'Address': '123 abc, IN, 50012'}, {'Phone': '123456'}],
'name2': [{'Address': '123 abc, IN, 50012'}, {'Phone': '123456'}]}

Related

Normalize Nested JSON structure using Python

Could someone help me getting the nested JSON to normalized JSON objects, I have deep nested JSON structure. I have seen examples using the flatten structure, However having one flatten structure doesnt help me in my case.
Sample dictionary:
emp = {
'name': "Paul",
'Age': '25',
'Location': "USA",
'Addresses':[
{
"longitude": "987",
"latitude": "765",
"postal_code": "90266" ,
"area" : [{"state": "CA"}]
},
{
"longitude": "123",
"latitude": "456",
"postal_ode": "1234" ,
"area" : [{"state": "NY"}]
}
]
}
expected Normalized objects
emp = ['name': "Paul", 'Age': '25','Location': "USA"}
emp_address = [
{ "longitude": "987", "latitude": "765","postal_code": "90266" },
{ "longitude": "123", "latitude": "456","postal_code": "1234" }
]
emp_address_area = [{"state": "CA"}, {"state": "NY"}]
I tried recursive functions to solve this problem, but no luck.
def normalized_json(y):
out = {}
def flatten(x, name=''):
if type(x) is dict:
for k, v in x.items():
if not isinstance (v, (dict, list)):
pass
flatten(v, name + k + '#')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '#')
i += 1
else:
out[name[:-1]] = x
flatten(y)
return out
print(normalized_json(emp))

Parsing nested JSON and collecting data in a list

I am trying to parse a nested JSON and trying to collect data into a list under some condition.
Input JSON as below:
[
{
"name": "Thomas",
"place": "USA",
"items": [
{"item_name":"This is a book shelf", "level":1},
{"item_name":"Introduction", "level":1},
{"item_name":"parts", "level":2},
{"item_name":"market place", "level":3},
{"item_name":"books", "level":1},
{"item_name":"pens", "level":1},
{"item_name":"pencils", "level":1}
],
"descriptions": [
{"item_name": "Books"}
]
},
{
"name": "Samy",
"place": "UK",
"items": [
{"item_name":"This is a cat house", "level":1},
{"item_name":"Introduction", "level":1},
{"item_name":"dog house", "level":3},
{"item_name":"cat house", "level":1},
{"item_name":"cat food", "level":2},
{"item_name":"cat name", "level":1},
{"item_name":"Samy", "level":2}
],
"descriptions": [
{"item_name": "cat"}
]
}
]
I am reading json as below:
with open('test.json', 'r', encoding='utf8') as fp:
data = json.load(fp)
for i in data:
if i['name'] == "Thomas":
#collect "item_name", values in a list (my_list) if "level":1
#my_list = []
Expected output:
my_list = ["This is a book shelf", "Introduction", "books", "pens", "pencils"]
Since it's a nested complex JSON, I am not able to collect the data into a list as I mentioned above. Please let me know no how to collect the data from the nested JSON.
Try:
import json
with open("test.json", "r", encoding="utf8") as fp:
data = json.load(fp)
my_list = [
i["item_name"]
for d in data
for i in d["items"]
if d["name"] == "Thomas" and i["level"] == 1
]
print(my_list)
This prints:
['This is a book shelf', 'Introduction', 'books', 'pens', 'pencils']
Or without list comprehension:
my_list = []
for d in data:
if d["name"] != "Thomas":
continue
for i in d["items"]:
if i["level"] == 1:
my_list.append(i["item_name"])
print(my_list)
Once we have the data we iterate over the outermost list of objects.
We check if the object has the name equals to "Thomas" if true then we apply filter method with a lambda function on items list with a condition of level == 1
This gives us a list of item objects who have level = 1
In order to extract the item_name we use a comprehension so the final value in the final_list will be as you have expected.
["This is a book shelf", "Introduction", "books", "pens", "pencils"]
import json
def get_final_list():
with open('test.json', 'r', encoding='utf8') as fp:
data = json.load(fp)
final_list = []
for obj in data:
if obj.get("name") == "Thomas":
x = list(filter(lambda item: item['level'] == 1, obj.get("items")))
final_list = final_list + x
final_list = [i.get("item_name") for i in final_list]
return final_list

how to get values of some values as type<String> which is inside a Map inside List in Dart/Flutter?

I am trying to extract the values which key equal Name in a key:value pair that i extracted from an API. I am using the following formula;
final List<String> namesList =list.map((e) => e["Name"].toString()).toList();
print(namesList);
I can extract the values of name keys, however it is extracted as JSArray. I couldn't extract them as string.
Result: [Name1, Name2] JSArray
Should be: ["Name1", "Name2"] List
Thank you.
Please refer to below code
void main() {
List list = [
{"code": "ABC", "Name": "\"Name1\"", "Type": "1"},
{"code": "DEF", "Name": "\"Name2\"", "Type": "2"}
];
final List<String> nameList = list.map((e) => e["Name"].toString()).toList();
print(nameList);
List list1 = [
{"code": "ABC", "Name": "Name1", "Type": "1"},
{"code": "DEF", "Name": "Name2", "Type": "2"}
];
final List<String> nameList1 =
list1.map((e) => '"${e["Name"].toString()}"').toList();
print(nameList1);
}
// Result
// ["Name1", "Name2"]
It seems that nameList element type is String.
The 'print' result only is shown remove double quotation marks.
Or you really want to make a list with double quotation marks.
void main() {
List list = [{'Code': 'ABC', 'Name': 'Name1', 'Type': '1'}, {'Code': 'DEF', 'Name': 'Name1', 'Type': '2'}];
final List<String> namesList =list.map<String>((e) => "\"${e["Name"]}\"").toList();
print(namesList);
print(namesList[0].runtimeType);
}

Converting JSON to CSV with missing fieldnames

I am having a hard time converting JSON to csv because the names on some of the records don't show up. For example:
[{device: 1,
name: 'John',
age: 25,
city: 'Denver'
},
{device: 2,
name: 'Jake',
age: 24,
city: 'New York'
},
{device: 3,
name: 'Phil',
age: 23}]
It is further made difficult because it's several thousand rows where sometimes the city is known, other times it's not.
I would like to put these together into a csv and just leave Phil's city blank.
You can use this:
import json
import csv
js = """[{"device": 1,
"name": "John",
"age": 25,
"city": "Denver"
},
{"device": 2,
"name": "Jake",
"age": 24,
"city": "New York"
},
{"device": 3,
"name": "Phil",
"age": 23}]
"""
js = json.loads(js)
with open( 'result.csv', 'w' ) as csv_file:
writer = csv.writer( csv_file )
columns = list({column for row in js for column in row.keys()})
writer.writerow( columns )
for row in js:
writer.writerow([None if column not in row else row[column] for column in columns])
This works even with different column names and larger numbers of columns!
There is probably a built in way to do this in the json to csv module but you could iterate over all your dicts and add the missing keys:
my_list = [{'device':1,
'name':'John',
'age':25,
'city': 'Denver' },
{'device':2,
'name':'Jake',
'age':24,
'city': 'New York'
},
{'device':3,
'name':'Phil',
'age':23}]
keys_and_default = {
'device': -1,
'name': 'N/A',
'age': -1,
'city': 'N/A'
}
for dic in my_list:
for key, val in keys_and_default.items():
if key not in dic:
dic[key] = val
print(my_list)

Python: Combine multiple lists into one JSON array

I want to merge several lists into one JSON array.
These are my two lists:
address = ['address1','address2']
temp = ['temp1','temp2']
I combine both lists by the following call and create a JSON .
new_list = list(map(list, zip(address, temp)))
jsonify({
'data': new_list
})
This is my result for the call:
{
"data": [
[
"address1",
"temp1"
],
[
"address2",
"temp2"
]
]
}
However, I would like to receive the following issue. How do I do that and how can I insert the identifier address and hello.
{
"data": [
{
"address": "address1",
"temp": "temp1"
},
{
"address": "address2",
"temp": "temp2"
}
]
}
You can use a list-comprehension:
import json
address = ['address1','address2']
temp = ['temp1','temp2']
d = {'data': [{'address': a, 'temp': t} for a, t in zip(address, temp)]}
print( json.dumps(d, indent=4) )
Prints:
{
"data": [
{
"address": "address1",
"temp": "temp1"
},
{
"address": "address2",
"temp": "temp2"
}
]
}
You can just change your existing code like this. That lambda function will do the trick of converting it into a dict.
address = ['address1','address2']
temp = ['temp1','temp2']
new_list = list(map(lambda x : {'address': x[0], 'temp': x[1]}, zip(address, temp)))
jsonify({
'data': new_list
})