how to extract data inside the list of maps and convert it into maps in dart - list

How can extract data inside the list of maps and convert it into maps in the dart.
Like I have a List of maps ================================================================================================================================================================================================
[
{"business_id":"2",
"business_title":"Spotify",
"business_phone":"(055) 3733783",
"business_email":"Usamashafiq309#gmail.com",
"business_website":"www.spotify.com",
"business_address":"Spotify AB, Regeringsgatans bro, Stockholm, Sweden",
"business_address_longitude":"18.0680873",
"business_address_latitude":"59.33096949999999",
"business_image":"5f84c7a4bbbd0121020201602537380.png",
"business_created_at":"2020-10-20 15:40:17",
"business_category_id":"2",
"cat_id":"2",
"cat_title":"Gym",
"cat_image":"280920201601308237.png"}
,{"business_id":"2",
"business_title":"Spotify",
"business_phone":"(055) 3733783",
"business_email":"Usamashafiq309#gmail.com",
"business_website":"www.spotify.com",
"business_address":"Spotify AB, Regeringsgatans bro, Stockholm, Sweden",
"business_address_longitude":"18.0680873",
"business_address_latitude":"59.33096949999999",
"business_image":"5f84c7a4bbbd0121020201602537380.png",
"business_created_at":"2020-10-20 15:40:17",
"business_category_id":"2",
"cat_id":"2",
"cat_title":"Gym",
"cat_image":"280920201601308237.png"}
]
and convert it like this
[ {"business_id":"2",
"business_title":"Spotify",},
{"business_id": "1",
"business_title": "Pizza Hut",},
]

You can use the map function to apply a function to each element of a list. Then you can create a submap with your map.
Here is a quick exemple:
void main() async {
List l = [
{
"business_id": "2",
"business_title": "Spotify",
"business_phone": "(055) 3733783",
},
{
"business_id": "1",
"business_title": "Pizza Hut",
"business_phone": "(055) 9999999",
}
];
print(extractMap(l));
}
List extractMap(List l) {
return l
.map((element) => Map.fromEntries([
MapEntry('business_id', element['business_id']),
MapEntry('business_title', element['business_title']),
]))
.toList();
}

Related

How to remove Map from list in dart

How can remove Map from list based on key value in map, in dart
void main() {
List<Map> names = [
{"id": 1, "name": "Bob"},
{"id": 2, "name": "Alex"},
];
names.forEach((element) {
element.keys.where((key) => element[key] == 1).forEach((names.remove));
});
print(names);
}
I try the above code but it does not works for me.
Thanks
names.removeWhere((element) => element["id"] == 1);

Mongodb conditional query search under an array

I have a data where an array is there. Under that array Many array of objects is there. I am mentioning the raw data so that anyone guess the structure
{
_id: ObjectId(dfs45sd54fgds4gsd54gs5),
content: [
{
str: "Hey",
isDelete: false
},
{
str: "world",
isDelete: true
}
]
}
So I want to search any string that match and I have top search under an array.
So my query is like this:
let searchTerm = req.body.key;
db.collection.find(
{
'content.str': {
$regex: `.*\\b${searchTerm}\\b.*`,
$options: 'i',
}
}
)
So this will return the data. Now for some reason I have to search the data if isDelete: false.
Right now it returns the data whether isDelete is true/false because I have not mentioned the conditon.
Can anyone help me out regarding this to get the data through condition. I want this to Mongodb Query only.
Any help is really appreciated.
The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria,
db.collection.find({
content: {
$elemMatch: {
isDelete: true,
str: {
$regex: `.*\\b${searchTerm}\\b.*`,
$options: "i"
}
}
}
},
{
"content.$": 1
})
Working Playground: https://mongoplayground.net/p/VkdWMnYtGA3
You can add another condition there as belo
db.test2.find({
$and: [
{
"content.str": {
$regex: "hey",
$options: "i",
}
},
{
"content.isDelete": false
}
]
},
{
'content.$':1 //Projection - to get only matching array element
})

Azure Cosmos query to convert into List

This is my JSON data, which is stored into cosmos db
{
"id": "e064a694-8e1e-4660-a3ef-6b894e9414f7",
"Name": "Name",
"keyData": {
"Keys": [
"Government",
"Training",
"support"
]
}
}
Now I want to write a query to eliminate the keyData and get only the Keys (like below)
{
"userid": "e064a694-8e1e-4660-a3ef-6b894e9414f7",
"Name": "Name",
"Keys" :[
"Government",
"Training",
"support"
]
}
So far I tried the query like
SELECT c.id,k.Keys FROM c
JOIN k in c.keyPhraseBatchResult
Which is not working.
Update 1:
After trying with the Sajeetharan now I can able to get the result, but the issue it producing another JSON inside the Array.
Like
{
"id": "ee885fdc-9951-40e2-b1e7-8564003cd554",
"keys": [
{
"serving": "Government"
},
{
"serving": "Training"
},
{
"serving": "support"
}
]
}
Is there is any way that extracts only the Array without having key value pari again?
{
"userid": "e064a694-8e1e-4660-a3ef-6b894e9414f7",
"Name": "Name",
"Keys" :[
"Government",
"Training",
"support"
]
}
You could try this one,
SELECT C.id, ARRAY(SELECT VALUE serving FROM serving IN C.keyData.Keys) AS Keys FROM C
Please use cosmos db stored procedure to implement your desired format based on the #Sajeetharan's sql.
function sample() {
var collection = getContext().getCollection();
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT C.id,ARRAY(SELECT serving FROM serving IN C.keyData.Keys) AS keys FROM C',
function (err, feed, options) {
if (err) throw err;
if (!feed || !feed.length) {
var response = getContext().getResponse();
response.setBody('no docs found');
}
else {
var response = getContext().getResponse();
var map = {};
for(var i=0;i<feed.length;i++){
var keyArray = feed[i].keys;
var array = [];
for(var j=0;j<keyArray.length;j++){
array.push(keyArray[j].serving)
}
feed[i].keys = array;
}
response.setBody(feed);
}
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
Output:

How can I convert json map to List String?

I need to get country name and country code into List as "Andorra (AD)". I can load json to a map but I cannot convert to List. How can I convert json map to List String?
"country": [
{
"countryCode": "AD",
"countryName": "Andorra",
"currencyCode": "EUR",
"isoNumeric": "020"
},
You can use the .map() function
var countryList = country.map((c) => '${c["countryName"]} (${c["countryCode"]})').toList()

Elastic search 5, search from list by sublist

I'm trying to search from an object that has a list property.
I need to be able to select all object that contains all sublist items.
ex :
If my object has [A,B,C] it should be returned for the given querys :
[A], [A,B], [A,B,C], [A,C], [C,A] ... (Input order doesn't have to match)
But if the sublist contains any element that is not part of the object list, it should not be returned.
ex :
[D], [A,D] ...
Those querys should not be valid.
I've managed to do it for the query with an existing sublist, but not when any item of the sublist doesn't exists.
Any ideas ?
Thanks !
Use comma seperate for sublist query item as a value for match query and set operator value to "and" as following:
Sample of document:
{
"Id": 1,
"Name": "One",
"tags": ["A","B","C"]
}
For sublist:[A,B]:
{
"query": {
"match": {
"tags": {
"query": "A,B",
"operator": "and"
}
}
}
}
I test in ElasticSearch 5.6.0 and 6.1.2
Assuming A, B, C, etc are mapped as keyword types, multiple bool query filter clauses would be one way
var response = client.Search<User>(s => s
.Query(q => +q
.Term(f => f.Badges, "A") && +q
.Term(f => f.Badges, "B") && +q
.Term(f => f.Badges, "C")
)
);
generates the following query
{
"query": {
"bool": {
"filter": [
{
"term": {
"badges": {
"value": "A"
}
}
},
{
"term": {
"badges": {
"value": "B"
}
}
},
{
"term": {
"badges": {
"value": "C"
}
}
}
]
}
}
}
A user document would need to have at least all of A, B and C badges to be considered a match.
A user document may well have other badges in addition to A, B and C; if you need to find documents that have exactly A, B and C, take a look at the terms_set query with a minimum_should_match* value set to the number of passed terms.