Convert list of nested dictionary into dictionary in Python - json

I have a list of nested dictionary as below. There is list inside the dictionary as well. I don't want to change the inner list but to turn the entire list into dictionary. The list is as below:
[{"id" : "A", "data" : [{"key_1" : 012},{"key_2" : 123}, {"key_3" : 234}]}]
I only want to change the list itself to dictionary without changing the inner list as below:
{{"id" : "A", "data" : [{"key_1" : 012},{"key_2" : 123}, {"key_3" : 234}]}}
Appreciate your help in this.

Use a dictionary comprehension and zip the list of dictionaries up with whatever list of keys you want. Here is an example using a subset of ascii lowercase letters:
{ pair[0]: pair[1] for pair in list(zip([*string.ascii_lowercase[0:len(input)]], input))}
>>> foo = [ { 'Age': 32,
'Emp_id': 'A',
'Gender': 'M',
'Result': [ {'Incentive': 3000, 'Month': 'Aug'},
{'Incentive': 3500, 'Month': 'Sep'},
{'Incentive': 2000, 'Month': 'Oct'}]},
{ 'Age': 35,
'Emp_id': 'B',
'Gender': 'M',
'Result': [{'Incentive': 1500, 'Month': 'Aug'}]},
{ 'Age': 31,
'Emp_id': 'C',
'Gender': 'F',
'Result': [ {'Incentive': 5000, 'Month': 'Aug'},
{'Incentive': 2400, 'Month': 'Sep'}]}]
>>> { pair[0]: pair[1] for pair in list(zip([*string.ascii_lowercase[0:len(input)]], input))}
>>> { 'a': { 'Age': 32,
'Emp_id': 'A',
'Gender': 'M',
'Result': [ {'Incentive': 3000, 'Month': 'Aug'},
{'Incentive': 3500, 'Month': 'Sep'},
{'Incentive': 2000, 'Month': 'Oct'}]},
'b': { 'Age': 35,
'Emp_id': 'B',
'Gender': 'M',
'Result': [{'Incentive': 1500, 'Month': 'Aug'}]},
'c': { 'Age': 31,
'Emp_id': 'C',
'Gender': 'F',
'Result': [ {'Incentive': 5000, 'Month': 'Aug'},
{'Incentive': 2400, 'Month': 'Sep'}]}}
note: you can pretty print code as follows:
import pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)

Related

How can I get all of x values from what I think is JSON

Python version: 3.10
Running a function returns this:
[{'type': 1, 'components': [{'type': 2, 'style': 1, 'label': 'She/Her', 'custom_id': 'She/Her'}, {'style': 1, 'label': 'He/Him', 'custom_id': 'He/Him', 'type': 2}]}]
How can I get all values of 'custom_id' within what is returned? Thank you!
You can do it like so:
myList = [{'type': 1, 'components': [{'type': 2, 'style': 1, 'label': 'She/Her', 'custom_id': 'She/Her'}, {'style': 1, 'label': 'He/Him', 'custom_id': 'He/Him', 'type': 2}]}]
for user in list(myList[0]["components"]):
print(user["custom_id"])
You can format your json here
https://jsonformatter.curiousconcept.com/
to see, wich list is in wich :)
#infinity wrote it similar.
[
{
"type":1,
"components":[
{
"type":2,
"style":1,
"label":"She/Her",
"custom_id":"She/Her"
},
{
"style":1,
"label":"He/Him",
"custom_id":"He/Him",
"type":2
}
]
}
]
myList = [{'type': 1, 'components': [{'type': 2, 'style': 1, 'label': 'She/Her', 'custom_id': 'She/Her'}, {'style': 1, 'label': 'He/Him', 'custom_id': 'He/Him', 'type': 2}]}]
for user in myList[0]['components']:
print(user['custom_id'])

Remove empty elements from nested JSON

I have a nested json with an arbitrary depth level :
json_list = [
{
'class': 'Year 1',
'room': 'Yellow',
'students': [
{'name': 'James', 'sex': 'M', 'grades': {}},
]
},
{
'class': 'Year 2',
'info': {
'teachers': {
'math': 'Alan Turing',
'physics': []
}
},
'students': [
{ 'name': 'Tony', 'sex': 'M', 'age': ''},
{ 'name': 'Jacqueline', 'sex': 'F' },
],
'other': []
}
]
I want to remove any element that its value meet certain criteria.
For example:
values_to_drop = ({}, (), [], '', ' ')
filtered_json = clean_json(json_list, values_to_drop)
filtered_json
Expected Output of clean_json:
[
{
'class': 'Year 1',
'room': 'Yellow',
'students': [
{'name': 'James', 'sex': 'M'},
]
},
{
'class': 'Year 2',
'info': {
'teachers': {
'math': 'Alan Turing',
}
},
'students': [
{ 'name': 'Tony', 'sex': 'M'},
{ 'name': 'Jacqueline', 'sex': 'F'},
]
}
]
I thought of something like first converting the object to string using json.dumps and then looking in the string and replacing each value that meets the criteria with some kind of flag to filter it after before reading it again with json.loads but I couldn't figure it out and I don't know if this is the way to go
I managed to get the desired output by tweaking this answer a bit:
def clean_json(json_obj, values_to_drop):
if isinstance(json_obj, dict):
json_obj = {
key: clean_json(value, values_to_drop)
for key, value in json_obj.items()
if value not in values_to_drop}
elif isinstance(json_obj, list):
json_obj = [clean_json(item, values_to_drop)
for item in json_obj
if item not in values_to_drop]
return json_obj

How to model class in order to serialize Dart Object (Map<String, List>) into JSON in Flutter?

I have a Map<Datetime, List> in Dart that stores events needed to fill up a calendar in my Flutter app:
final Map<DateTime, List> _events = {
DateTime(2020, 7, 7): [
{'name': 'Event A', 'isDone': true},
],
DateTime(2020, 7, 11): [
{'name': 'Event A', 'isDone': true}
],
DateTime(2020, 7, 9): [
{'name': 'Event A', 'isDone': true},
{'name': 'Event B', 'isDone': true},
],
DateTime(2020, 7, 10): [
{'name': 'Event A', 'isDone': true},
{'name': 'Event B', 'isDone': true},
],
DateTime(2020, 7, 13): [
{'name': 'Event A', 'isDone': true},
{'name': 'Event B', 'isDone': true},
{'name': 'Event C', 'isDone': false},
],
DateTime(2020, 7, 25): [
{'name': 'Event A', 'isDone': true},
{'name': 'Event B', 'isDone': true},
{'name': 'Event C', 'isDone': false},
],
DateTime(2020, 7, 6): [
{'name': 'Event A', 'isDone': false},
],
};
I want to convert this into JSON but I'm not entirely sure how to model the class. Anyone got any ideas?
If you simply want to convert this to a JSON you can use the jsonEncode function and use the toEncodable named parameter to allow your object to be encoded. I made an example here that just cast the keys of your Map to a String so that it can be encoded.
jsonEncode(
_events,
toEncodable: (input) {
return _events.map((key, value) {
return MapEntry(key.toString(), value);
});
}
);
When you want to decode this back to your Map<DateTime, List> you can just do the reverse.
var objectTemp = jsonDecode(
json,
);
var output = objectTemp.map((key, value) {
return MapEntry(DateTime.parse(key), value);
})
Create a model class for Event and store the event data in it (name, isDone and date). Then you put them in a List<Event>. You can search on this list for a spesific date and get the according events if any. Then you can make that data class serializable.

How to return Nested TreeView JSON in Web API

I'm new to web API. I need some help to generate the JSON like following.
[
{
'id': 66,
'text': 'This is the first comment.',
'creator': {
'id': 52,
'display_name': 'Ben'
},
'respondsto': null,
'created_at': '2014-08-14T13:19:59.751Z',
'responses': [
{
'id': 71,
'text': 'This is a response to the first comment.',
'creator': {
'id': 14,
'display_name': 'Daniel',
},
'respondsto': 66,
'created_at': '2014-08-14T13:27:13.915Z',
'responses': [
{
'id': 87,
'text': 'This is a response to the response.',
'creator': {
'id': 52,
'display_name': 'Ben',
},
'respondsto': 71,
'created_at': '2014-08-14T13:27:38.046Z',
'responses': []
}
]
}
]
},
{
'id': 70,
'text': 'Đây là bình luận thứ hai.',
'creator': {
'id': 12,
'display_name': 'Nguyễn'
},
'respondsto': null,
'created_at': '2014-08-14T13:25:47.933Z',
'responses': []
}
];
My Intention is to give JSON Data for the Image.
I'm able to generate normal JSON data. I was struck how to create that responses inside responses until the empty response comes.
Any help would be appreciated.
UPDATE: I found the Answer

Represent graph data as a key-value object

I'm starting to dig into graph databases, but i have no idea, how these graphs are stored internally. Let's say i have this graph (taken from Wikipedia):
How do i serialize this graph as a key-value object? (a Python dict, for example)
I imagine two dicts, one for vertices and one for edges:
{'vertices':
{'1': {'Name': 'Alice', 'Age': 18},
'2': {'Name': 'Bob', 'Age': 22},
'3': {'Type': 'Group', 'Name': 'Chess'}},
'edges':
{'100': {'Label': 'knows', 'Since': '2001/10/03'},
'101': {'Label': 'knows', 'Since': '2001/10/04'},
'102': {'Label': 'is_member', 'Since': '2005/7/01'},
'103': {'Label': 'Members'},
'104': {'Label': 'Members'},
'105': {'Label': 'is_member', 'Since': '2011/02/14'}},
'connections': [['1', '2', '100'], ['2', '1', '101'],
['1', '3', '102'], ['3', '1', '103'],
['3', '2', '104'], ['2', '3', '105']]}
But i'm not sure, whether this is the most practical implementation. Maybe the "connections" should be inside "vertices" dict. So, what is the best way to implement graph datastore using key-value objects? What and where can i read more about it?
Possibly related, but not a duplicate: How to represent a strange graph in some data structure
The normal pattern is to not have a separate connections structure but to put that information in the edges structure. This gives something like:
{
'vertices': {
'1': {'Name': 'Alice', 'Age': 18},
'2': {'Name': 'Bob', 'Age': 22},
'3': {'Type': 'Group', 'Name': 'Chess'} },
'edges': [
{'from': '1', 'to': '2', 'Label': 'knows', 'Since': '2001/10/03'},
{'from': '2', 'to': '1', 'Label': 'knows', 'Since': '2001/10/04'},
{'from': '1', 'to': '3', 'Label': 'is_member', 'Since': '2005/7/01'},
{'from': '3', 'to': '1', 'Label': 'Members'},
{'from': '3', 'to': '2', 'Label': 'Members'},
{'from': '2', 'to': '3', 'Label': 'is_member', 'Since': '2011/02/14'} ] }
seems ok - each object has its it, there is no duplications. it's good for 'read and process purpose'. but there is no 'best' representation. it always depends on your purpose. do you want to be able to quickly find vertices by name? or edges by date? or maybe you want to quickly test if two vertices are connected? or the opposite - you want to quickly modify some parts of the graph? each purpose requires different data structures of database tables
how these graphs are stored internally
how do I serialize this graph as a key-value object
These questions are different and they need different answers.
In the former case, the main requirement is probably to perform complex queries efficiently.
I'd suggest to investigate existing industrial-strength solutions.
In NoSQL terms, these nested key-value objects are documents. Hence, one could look into how graphs are stored in "layered" multi-model databases that:
support graph data model, and
use underlying document data model.
Examples of such databases are ArangoDB, OrientDB, Azure CosmosDB.
You could also replace "document data model" with "wide column data model", because wide column data model can be conidered as two-dimensional key-value model.
Examples of such databases are DataStax Enterprise Graph and perhaps Grakn.
For instance, in ArangoDB, edges are stored as regular documents, but in special collections.
Obviously, data structures used may be accompanied with additional indexes etc. (or not).
So, what is the best way to implement graph datastore using key-value objects?
What and where can i read more about it?
I'd suggest another one article from ArangoDB:
Storing a graph in a pure document store
I'd make few changes in Eamonn's answer.
Every vertex and edge has 3 things.. id, Label and Properties
{
'vertices': {
'1': {'Label' : Person, 'Properties' : { 'Name': 'Alice', 'Age': 18}},
'2': {'Label' : Person, 'Properties' : {'Name': 'Bob', 'Age': 22}},
'3': {'Label': 'Group', 'Properties' : { 'Name': 'Chess'} },
'edges': [
'4' : {'from': '1', 'to': '2', 'Label': 'knows', 'Properties':{'Since': '2001/10/03' , 'Until' : '2001/10/03'}},
'5' : {'from': '2', 'to': '1', 'Label': 'knows', 'Properties':{'Since': '2001/10/04', 'Until' : '2001/10/05'}}
]
}
This way you can do query by vertex/edge, and their Labels and their properties.
I would serialize it like this, except you should choose the keys based on what you are looking up by. I assumed you are using the id, but perhaps using the name could be better.
{
'members': {
'1': {
'id': '1',
'name': 'Alice',
'age': 18,
'groups': {
'3': {
'path': 'groups.3',
'since': '2005-07-01'
}
},
'knows': {
'2': {
'path': 'members.2',
'since': '2001-10-03'
}
}
},
'2': {
'id': '2',
'name': 'Bob',
'age': 22,
'groups': {
'3': {
'path': 'groups.3',
'since': '2011-02-14'
}
},
'knows': {
'1': {
'path': 'members.1',
'since': '2001-10-04'
}
}
}
},
'groups': {
'3': {
'id': '3',
'name': 'Chess',
'members': {
'1': { 'path': 'members.1' },
'2': { 'path': 'members.2' }
}
}
}
}
You can serialize graphs directly into key-value pairs if you have a way of serializing references to other parts of the graph, which is what I use 'path' for. If I was deserializing it into a dict, I may consider replacing the path values with the actual dictionaries they refer to. Keep in mind that this may cause circular references which could cause problems if you were serializing it into json or something.
I would add an adjacency to the structure too. My take would be like this,
{
'vertices': {
'1': {'Name': 'Alice', 'Age': 18},
'2': {'Name': 'Bob', 'Age': 22},
'3': {'Type': 'Group', 'Name': 'Chess'}
},
'edges': {
'100' : {'from': '1', 'to': '2', 'Label': 'knows', 'Since': '2001/10/03'},
'101': {'from': '2', 'to': '1', 'Label': 'knows', 'Since': '2001/10/04'},
....
},
'adjacency': {
'1': ['101', '102'],
...
}
}
This way I can easily find which edges are adjacent to my vertices instead of iterating through all the edges.