newbie here. would anyone know how can i match the value of list x to the value of list y's key["id"]?
thank you in advance! appreciate any help. =)
list x = ["open_box", "close_box"];
list y =
[
{
"id": "open_box",
"name": "Open",
"action": ["open_box"]
},
{
"id": "close_box",
"name": "Close",
"action": ["open_box", "close_box", "pull_box"]
},
{
"id": "pull_box",
"name": "Pull",
"action": ["open_box", "close_box", "pull_box", "push_box"]
}
]
loop through x using for in, and then finally you can use where method from List
for (String action in x) {
dynamic temp = y.where((data) => data['id'] == action);
if (temp.isNotEmpty) {
// do someting
print(temp);
}
}
Ref: Dart Documentation - List
List provides a where method that allows you to filter the list based on a condition. Here I am using the where method of the y list and searching the x list for the id value of the current item in the y list. I check the length of the search for the x list and if it is greater than zero, then we have found a match inside the x list for that id. z will hold the list of items that have matched the ids from the x list inside the y list.
List x = ["open_box", "close_box"];
List y =
[
{
"id": "open_box",
"name": "Open",
"action": ["open_box"]
},
{
"id": "close_box",
"name": "Close",
"action": ["open_box", "close_box", "pull_box"]
},
{
"id": "pull_box",
"name": "Pull",
"action": ["open_box", "close_box", "pull_box", "push_box"]
}
];
var z = y.where((data) => x.where((value) => value==data["id"]).length>0);
print(z);
Here is the expected output
I/flutter ( 6086): ({id: open_box, name: Open, action: [open_box]}, {id: close_box, name: Close, action: [open_box, close_box, pull_box]})
list x = ["open_box", "close_box"];
list y =
[
{
"id": "open_box",
"name": "Open",
"action": ["open_box"]
},
{
"id": "close_box",
"name": "Close",
"action": ["open_box", "close_box", "pull_box"]
},
{
"id": "pull_box",
"name": "Pull",
"action": ["open_box", "close_box", "pull_box", "push_box"]
}
];
var i,j;
var yy=jsonDecode(y);
for(j=0;j<=x.length;j++){
for(i=0;i<=yy.length;i++){
if(yy[i]['id'].contains(x[j])){
//Do something if true
print(yy[i]);
}
}
}
This code compare x[0] means "open_box" to all List of Maped data of y it will print index value where conditions true
Related
I'm trying to separate a list from map item and also another map which is exist on my map item i'm beginner on dart?
someone help me how can do it?
Here is my list
var studentList2 = [
{
"Name": "Gias Uddin Samir",
"Id": "110",
"Result": [
{"Phy": "3.0", "Che": "4.0"}
],
"Completed": ["sp-2017", "sum-2017", "fall-2017"]
},
{
"Name": "Gias Uddin Samir",
"Id": "110",
"Result": [
{"Phy": "3.0", "Che": "4.0"}
],
"Completed": ["sp-2018", "sum-2018", "fall-2018"]
},
{
"Name": "Gias Uddin Samir",
"Id": "110",
"Result": [
{"Phy": "3.0", "Che": "4.0"}
],
"Completed": ["sp-2019", "sum-2019", "fall-2019"]
}
];
I want to separate Result list
"Result": [
{"Phy": "3.0", "Che": "4.0"}
],
ans also
"Completed": ["sp-2019", "sum-2019", "fall-2019"]
Maybe through the map method:
final results = studentList2.map((item) => item['Result']).toList();
print(results);
// [[{Phy: 3.0, Che: 4.0}], [{Phy: 3.0, Che: 4.0}], [{Phy: 3.0, Che: 4.0}]]
final completed = studentList2.map((item) => item['Completed']).toList();
print(completed);
// [[sp-2017, sum-2017, fall-2017], [sp-2018, sum-2018, fall-2018], [sp-2019, sum-2019, fall-2019]]
a={
"car": "Nissan",
"from": [
{
"Japan1": "People1",
"make_type": "allow",
"driver": {
"id": "12345",
"name": "user1",
"type": "user"
}
},
{
"Japan1": "People1",
"make_type": "allow",
"driver": {
"id": "98765",
"name": "user2",
"type": "user"
}
}
]}
Objective: want to remove "name" and "type" inside "driver"
I have tried a lot of method, if use print the name or type, just put into a new var level by level.
But how can I delete it?
Thanks
You can use del:
for f in a["from"]:
del f["driver"]["name"]
del f["driver"]["type"]
print(a)
Prints:
{
"car": "Nissan",
"from": [
{"Japan1": "Peop1", "make_type": "plastic", "driver": {"id": "12345"}}
],
}
Or:
for f in a["from"]:
f["driver"] = {
k: v for k, v in f["driver"].items() if k not in {"name", "type"}
}
Thanks for your help,
I'm trying to transform the Array called 'Tags' hosting a list of pair of key-values into a list of columns: CustomerId, CustomerDisplayName, CustomerPath where each list contains a respective value being a store for each charge.
{
"periodFrom": "2020-11-09T00:00:00",
"periodTo": "2020-12-08T00:00:00",
"charges": [
{
"listPrice": 5.05,
"netPrice": 5.05,
"netPriceProrated": 5.05,
"subTotal": 5.05,
"currency": "CAD",
"isBilled": true,
"isProratable": true,
"deductions": [],
"fees": [],
"invoice": {
"number": "2822835",
"date": "2020-11-16T00:00:00",
"periodFrom": "2020-10-09T00:00:00",
"periodTo": "2020-11-08T00:00:00"
},
"taxes": [
{
"name": "GST",
"appliedRate": 5.0
},
{
"name": "QST",
"appliedRate": 9.975
}
],
"tags": [
{
"name": "CustomerId",
"value": "42c8edf4-365a-4068-bde6-33675832afbb"
},
{
"name": "CustomerDisplayName",
"value": "Blue Sky Group"
},
{
"name": "CustomerPath",
"value": "Devmesh Co-Branded/Blue Sky Group"
}
]
}
]
}
Here the actual snippet of code I'm actually
let
...
Response2 = Table.FromRecords( { Response } ),
#"Expand1" = Table.ExpandListColumn(Response2, "charges"),
#"Expand2" = Table.ExpandRecordColumn(#"Expand1", "charges", {"productId", "productName", "sku", "chargeId", "chargeName", "chargeType", "periodFrom", "periodTo", "quantity", "listPrice", "netPrice", "netPriceProrated", "subTotal", "currency", "isBilled", "isProratable", "deductions", "fees", "invoice", "taxes", "tags"}, {"charges.productId", "charges.productName", "charges.sku", "charges.chargeId", "charges.chargeName", "charges.chargeType", "charges.periodFrom", "charges.periodTo", "charges.quantity", "charges.listPrice", "charges.netPrice", "charges.netPriceProrated", "charges.subTotal", "charges.currency", "charges.isBilled", "charges.isProratable", "charges.deductions", "charges.fees", "charges.invoice", "charges.taxes", "charges.tags"})
in
#"Expand2"
I'm getting JSON data from webservice and trying to make a table . Datadisk is presented as List and clicking into each item will navigate further down the hiearchy like denoted in screenshots below. I need to concatate storageAccountType for each item with | sign, so if there were 2 list items for Greg-VM and it had Standard_LRS for first one and Premium_LRS for second one then new column will list Standard_LRS | Premium_LRS for that row.
Input returned by function is below
[
{
"name": "rhazuremspdemo",
"disk": {
"id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/AzureMSPDemo/providers/Microsoft.Compute/disks/rhazuremspdemo_OsDisk_1_346353b875794dd4a7a5c5938abfb7df",
"storageAccountType": "StandardSSD_LRS"
},
"datadisk": []
},
{
"name": "w12azuremspdemo",
"disk": {
"id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/AzureMSPDemo/providers/Microsoft.Compute/disks/w12azuremspdemo_OsDisk_1_09788205f8eb429faa082866ffee0f18",
"storageAccountType": "Premium_LRS"
},
"datadisk": []
},
{
"name": "Greg-VM",
"disk": {
"id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Greg-VM_OsDisk_1_63ed471fef3e4f568314dfa56ebac5d2",
"storageAccountType": "Premium_LRS"
},
"datadisk": [
{
"name": "Data",
"createOption": "Attach",
"diskSizeGB": 10,
"managedDisk": {
"id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Data",
"storageAccountType": "Standard_LRS"
},
"caching": "None",
"toBeDetached": false,
"lun": 0
},
{
"name": "Disk2",
"createOption": "Attach",
"diskSizeGB": 10,
"managedDisk": {
"id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Disk2",
"storageAccountType": "Standard_LRS"
},
"caching": "None",
"toBeDetached": false,
"lun": 1
}
]
}
]
How do I do that?
Thanks,
G
This should help you. It steps through the process.
If you have a scenario like this
you can use Add custom Column and type the follwing expression:
=Table.Columns([TableName], "ColumnName")
to get it as list:
Now you can left click on the Custom column and chose Extract Values....
Choose Custom and your delimiter | and hit OK
This way the data who was in your list will now be in the same row with the delimiter
I have a JSON file, in that three objects are available, In that 2nd and 3rd objects does not have some fields which I actually needed. In missing fields, I need to add my own values. I will provide my code below
I tried this So far:
with open("final.json") as data1:
a = json.load(data1)
final = []
for item in a:
d = {}
d["AppName"]= item["name"]
d["AppId"] = item["id"]
d["Health"] = item["health"]
d["place1"] = item["cities"][0]["place1"]
d["place2"] = item["cities"][0]["place2"]
print(final)
Error: I am getting Key Error
My Input JSON file has similar data:
[{
"name": "python",
"id": 1234,
"health": "Active",
"cities": {
"place1": "us",
"place2": "newyork"
}
},
{
"name": "java",
"id": 2345,
"health": "Active"
}, {
"name": "python",
"id": 1234
}
]
I am expecting output:
[{
"name": "python",
"id": 1234,
"health": "Active",
"cities": {
"place1": "us",
"place2": "newyork"
}
},
{
"name": "java",
"id": 2345,
"health": "Null",
"cities": {
"place1": "0",
"place2": "0"
}
}, {
"name": "python",
"id": 1234,
"health": "Null",
"cities": {
"place1": "0",
"place2": "0"
}
}
]
I see two issues with the code that you have posted.
First, you are referring to the 'cities' field in you input JSON as if it is a list when it is, in fact, an object.
Second, to handle JSON containing objects which may be missing certain fields, you should use the Python dictionary get method. This method takes a key and an optional value to return if the key is not found (default is None).
for item in a:
d = {}
d["AppName"]= item["name"]
d["AppId"] = item["id"]
d["Health"] = item.get("health", "Null")
d["place1"] = item.get("cities", {}).get("place1", "0")
d["place2"] = item.get("cities", {}).get("place2", "0")