I have a dictionary that contains a list of dictionaries, of which one of the elements (C) contains another list of dictionaries. The structure is as follows:
{
"fruits":
[
{
"A": "apple",
"B": "banana",
"C":
[
{
"D": "Green grape",
"E": "Red grape",
}
{
"D": "Orange",
"E": "Grapefruit",
}
]
}
],
"vegetables":
etc...
}
I would like to move the elements D & E up a hierarchy while removing C. The resulting dictionary should look as follows.
{
"fruits":
[
{
"A": "apple",
"B": "banana",
"D": "Green grape",
"E": "Red grape",
},
{
"A": "apple",
"B": "banana",
"D": "Orange",
"E": "Grapefruit",
}
]
"vegetables":
etc...
}
Any suggestions?
Related
Sorry for my poor explanation, I just started learning dart.
With a mock service and a json file I created a set amount of items
Example:
{
"items": [
{
"id": "01",
"type": "a"
},
{
"id": "02",
"type": "b"
},
{
"id": "03",
"type": "c"
}
]
}
when creating the list on the service it creates a single list like this:
if (json['items'] != null) {
final itemList = <ItemList>[];
json['items'].forEach((v) {
itemlistList.add(ItemList.fromJson(v));
});
return ItemList;
} else {
return [];
}
is there a way to, form the create list step to already separate them into 3 different lists for the type a, b, and c items? and if now, where and how would I divide this itemlist into 3 based on the type characteristic of each item?
Using groupBy, as suggested in this extremely similar question: Flutter/Dart how to groupBy list of maps?
import "package:collection/collection.dart";
main(List<String> args) {
var data = [
{"id": "01", "type": "a"},
{"id": "02", "type": "b"},
{"id": "03", "type": "c"},
{"id": "04", "type": "a"},
{"id": "05", "type": "a"},
{"id": "06", "type": "b"},
];
var newMap = groupBy(data, (Map obj) => obj["type"]);
print(newMap);
}
I need to find all the results that start with certain input for example for the inputs: "Paul", "pau", "paul Gr", "Paul Green", "Paul Gree" , "Pel", "pele", "joh","john" etc.. The search has to be case insensive..
it suppose to return all of these(the input search string is at least 3 characters long):
[
{
"_id": ObjectId("5e6ffe413f71835ae3aa4b60"),
"f": "Paul",
"id": 11811,
"l": "Pelè",
"r": 64
},
{
"_id": ObjectId("5e6ffe413f71835ae3aa4b65"),
"f": "paul",
"id": 11811,
"l": "walker",
"r": 64
},
{
"_id": ObjectId("5e6ffe413f71835ae3aa4b66"),
"f": "johnny",
"id": 11811,
"l": "Green",
"r": 64
}
]
tried to do the following:
contain_searched_term_players = list(db.players_collection.find({'$or': [{'f': {'$regex': searched_player_name_string, '$options': 'i'}},
{'l': {'$regex': searched_player_name_string, '$options': 'i'}},
{'c': {'$regex': searched_player_name_string, '$options': 'i'}}]}).sort([{'r', -1}])
but it doesnt work for "Paul Green"
searched_player_name_string is the given input(the inputs above, for example Paul Green)
You need to provide correct Regex for query condition
^(Paul Green|Paul Gree|Paul|paul|pau|Gr|pele|Pel|john|joh)
RegexPlayground
searched_player_name_string = "^(Paul Green|Paul Gree|Paul|paul|pau|Gr|pele|Pel|john|joh)"
result_cursor = db.players_collection.find({
"$or": [
{
"f": {
"$regex": searched_player_name_string,
"$options": "i"
}
},
{
"l": {
"$regex": searched_player_name_string,
"$options": "i"
}
},
{
"c": {
"$regex": searched_player_name_string,
"$options": "i"
}
}
]
})
searched_player_name_string = list(result_cursor)
MongoPlayground
Split your input in separate strings, run the query on each and append the results together (checking first it's not already found), Finally sort the results:
searched_player_name_string = 'Paul Green'
found_players = []
for regex in searched_player_name_string.split():
contain_searched_term_players = db.players_collection.find({'$or': [{'f': {'$regex': regex, '$options': 'i'}},
{'l': {'$regex': regex, '$options': 'i'}},
{'c': {'$regex': regex, '$options': 'i'}}]})
for player in contain_searched_term_players:
# The next line avoids creating duplicate answers if there are multiple matches for the same player
if player['_id'] not in [ o['_id'] for o in found_players ]:
found_players.append(player)
# Sort the output by "r" - highest first
pprint.pprint(sorted(found_players, key=lambda o: o['r'], reverse=True))
I am trying to write Json using property tree. This is what I have tried:
boost::property_tree::propTree propTree1, propTree2, propTree3, propTree4, propTree5, propTree6, propTree7, propTree8, propTree9, propTree10;
propTree1.put( "a", "" );
propTree1.put( "b", "" );
propTree1.put( "c", "" );
propTree2.push_back(std::make_pair("", propTree1));
propTree3.put("l", "");
propTree3.put("m", "");
propTree4.push_back(std::make_pair("", propTree3));
propTree1.add_child("Msg", propTree4);
propTree5.put("r", "");
propTree5.put("s", "");
propTree6.push_back(std::make_pair("", propTree5));
propTree1.add_child("Msg1", propTree6);
propTree1.add_child("Msg2", propTree8);
propTree9.put("t", "");
propTree9.put("u", "");
propTree10.push_back(std::make_pair("", propTree9));
propTree1.add_child("Msg3", propTree10);
boost::property_tree::write_json( "OUTPUT", propTree1 );
The Output I am getting is below:
{
"a": "",
"b": "",
"Msg": [
{
"l": "",
"m": ""
}
],
"Msg1": [
{
"r": "",
"s": ""
}
],
"Msg2": "",
"Msg3": [
{
"t": "",
"u": ""
}
]
}
Expected output is below: (Commented are the questions)
{ "Msg0":{ //Missing in my output
"a": "",
"b": "",
"Msg": [
{
"l": "",
"m": ""
}
],
"Msg1": [
{
"r": "",
"s": "",
"abc": [ // Missing this "abc" array within Msg1 array
"note1", //Output with values (no keys like "r" & "s")
"note2", // Can we pass setlist/stringstream in "abc" array?
"note3",
]
}
],
"Msg2": "",
"Msg3": [
{
"t": "",
"u": ""
}
]
}
}
1) I am not able to get the format of JSON which is mentioned above. Please check the comments.
2) How should I add string/stringstream/setofnames in "abc" array? Which data type can be passed here and how to achieve that?
3) Also is there any efficient way of doing the above work?
Can anyone help me with the above 3 questions? Thanks.
http://www.django-rest-framework.org/api-guide/relations/#nested-relationships
It is not clear from the documentation if and how it's possible to use the same principle of nested relationships to render a flat JSON.
For example:
"nest": {
"b": {
"c": {
"d": {
"e": {
"E": "echo"
},
"D": "delta"
},
"C": "charlie"
},
"B": "beta"
},
"A": "alpha"
}
"flat": {
"A": "alpha",
"B": "beta",
"C": "charlie",
"D": "delta",
"E": "echo"
}
How can the flat JSON be achieved?
Lets assume your code looks like
mymodel_queryset = MyModel.objects.all()
So what you can do is write your own custom serializer as follows:
def my_custom_serializer(queryset):
res = []
for q in queryset:
ob = {'name': q.name, 'city': q.city.name} # Notice how i use the relation here for city. So the nested relation is becoming flat for city
res.append(ob)
return res
Now in your code you can use
data = my_custom_serializer(mymodel_queryset)
return Response(status=status.HTTP_200_OK, data=data)
Hope this helps you. :)
I have created created a dictionary from Django-mptt and used json.dump() to get the JSON object.
{
"a": "cat1",
"c": ["item2", "item1"],
"b": [
{"a": "burgers", "c": [], "b": []},
{"a": "south indian", "c": [], "b": []},
{"a": "veg subs", "c": ["corn and peas", "a;loo patty", "paneer delite"], "b": []},
{"a": "traditional", "c": ["subway melt", "Chicken ranch", "turkey", "Subway club"], "b": []},
{"a": "favourites", "c": ["tuna", "chicken ham", "roasted chicken"], "b": []},
{"a": "beverages", "c": [], "b": []},
{"a": "north indian", "c": [], "b": []},
{"a": "oriental", "c": [], "b": []},
{"a": "european ", "c": [], "b": []}
]
}
Here, "cat1" is the main category, having sub-categories children, burgers, traditional, favourites etc. Any category can have sub-categories.
a - Category name
c - Items list
b - Children
This is my JSON object. What do I use to show this JSON object in tree format in JavaScript.
I need to display the First children of the every category clicked. And for every Category clicked, I want to display the items below the tree structure.