I have a json nested tree that looks like this :
{
"children": [
{
"children": [],
"id": 9,
"level": 1,
"name": "Zone d'audit Interne"
},
{
"children": [],
"id": 10,
"level": 1,
"name": "Magasin PF"
},
{
"children": [],
"id": 11,
"level": 1,
"name": "Magasin MP"
},
{
"children": [
{
"children": [],
"id": 14,
"level": 2,
"name": "Packaging"
},
{
"children": [],
"id": 15,
"level": 2,
"name": "Labelling"
},
{
"children": [],
"id": 16,
"level": 2,
"name": "Assemblage Acc"
}
],
"id": 12,
"level": 1,
"name": " U4: Assembalge Finale"
},
{
"children": [
{
"children": [],
"id": 18,
"level": 2,
"name": "Contrôle traitement"
},
{
"children": [],
"id": 19,
"level": 2,
"name": "Tunnel de Séchage"
},
{
"children": [],
"id": 20,
"level": 2,
"name": "Traitement Mécanique"
}
],
"id": 17,
"level": 1,
"name": "U3: Traitement"
},
{
"children": [
{
"children": [],
"id": 23,
"level": 2,
"name": "Ass_Ligne 2"
},
{
"children": [],
"id": 24,
"level": 2,
"name": "Ass_ligne 1"
},
{
"children": [],
"id": 39,
"level": 2,
"name": "BU"
}
],
"id": 21,
"level": 1,
"name": "U2: Assemblage SF"
},
{
"children": [
{
"children": [],
"id": 26,
"level": 2,
"name": "Mgasin Tampon"
},
{
"children": [],
"id": 27,
"level": 2,
"name": "Contrôle Kitting"
}
],
"id": 25,
"level": 1,
"name": "U1: Kitting"
},
{
"children": [],
"id": 35,
"level": 1,
"name": "to-delete"
}
],
"id": 8,
"level": 0,
"name": "DEMO"
}
I've tried parsing it but I only figured out how to parse the first level.
I want to be able to parse it and print / use every node's level .
if possible I want to turn it into a list of objects so I can use it in my widgets,
If anyone can help I'd be grateful
here's how am parsing it :
Future<Map<String, dynamic>?> fetchPerimeterTree(
{required int id, required String baseUrl}) async {
try {
final request = http.Request(
'GET', Uri.parse("$baseUrl${Endpoints.fetchPerim}$id/json"));
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
debugPrint("Perimeters tree fetched with success");
var jsonData = jsonDecode(await response.stream.bytesToString());
List<dynamic> fetchData2 = jsonData["children"];
fetchData2.forEach((element) {
print(element["name"]);
print(element["children"]);
print(element["level"]);
});
print(fetchData2);
return jsonData;
} else {
throw Exception('Failed to load perimeter tree ${response.statusCode}');
}
} catch (ex) {
debugPrint(ex.toString());
}
}
Assumed your json called jsonData, and define a class model like this:
class ChildModel {
final int id;
final int level;
final String name;
final List<ChildModel>? children;
ChildModel(
{required this.id,
required this.level,
required this.name,
this.children});
static ChildModel fromJson(Map<String, dynamic> json) {
List children = json["children"] as List;
return ChildModel(
id: json["id"],
level: json["level"],
name: json["name"],
children: children.isNotEmpty
? children.map((e) => ChildModel.fromJson(e)).toList()
: []);
}
}
then you can get your data like this:
ChildModel result = ChildModel.fromJson(jsonData);
and you can test it like this:
print(" name = ${result.children?[3].children?[0].name}"); // name = Packaging
In this way you can cache all the layers of children.
Related
I start a new video with JSON-RPC call:
{"jsonrpc": "2.0",
"method": "Player.Open",
"params":
{"item":
{"file": "c:\\MyCodes\\samples\\sample-6s.mp4" }},
"id": 1
}
Video started and I want to get the video details and use the following Player.GetItem method:
{
"jsonrpc": "2.0",
"method": "Player.GetItem",
"params": {
"properties": [
"title",
"album",
"artist",
"season",
"episode",
"duration",
"showtitle",
"tvshowid",
"thumbnail",
"file",
"fanart",
"streamdetails"
],
"playerid": 1
},
"id": "VideoGetItem"
}
The problem I got back this:
{
"id": "VideoGetItem",
"jsonrpc": "2.0",
"result": {
"item": {
"album": "",
"artist": [],
"episode": -1,
"fanart": "",
"file": "c:\\MyCodes\\samples\\sample-6s.mp4",
"label": "sample-6s.mp4",
"season": -1,
"showtitle": "",
"streamdetails": {
"audio": [],
"subtitle": [],
"video": []
},
"thumbnail": "",
"title": "",
"tvshowid": -1,
"type": "unknown"
}
}
}
but if I start the same video from KODI GUI I got back this:
{
"id": "VideoGetItem",
"jsonrpc": "2.0",
"result": {
"item": {
"album": "",
"artist": [],
"episode": -1,
"fanart": "",
"file": "C:\\MyCodes\\samples\\sample-6s.mp4",
"label": "sample-6s.mp4",
"season": -1,
"showtitle": "",
"streamdetails": {
"audio": [
{
"channels": 2,
"codec": "mp3",
"language": ""
}
],
"subtitle": [],
"video": [
{
"aspect": 1.3333330154418946,
"codec": "mpeg4",
"duration": 2701,
"hdrtype": "",
"height": 384,
"language": "",
"stereomode": "",
"width": 512
}
]
},
"thumbnail": "image://video#C%3a%5cMyCodes%5csamples%5csample-6s.mp4/",
"title": "",
"tvshowid": -1,
"type": "unknown"
}
}
}
Any idea, or experience? Maybe I should use a different method to get the currently running video details?
I have an use case, where user is defined a workflow. As user hits the API, step function gets started. A lambda returns a list of steps based on users & start the workflow e.g.:
step1. it process(extracts) the doc,
step2. inserts some data to
DB
step3. .....
step4. .....
here steps are in map, as these steps are returned from lambda.
Is there any way to get response from each map iteration, so that I can show user at which step the process is , & the output of previous step.
here is my step function design:
test-fetch-input-workflow: returns an array: [step1, step2, ....]
workflow step-lambda: calls some lambda based on step name
Assuming that you are using Standard Workflows, you can get this information using the GetExecutionHistory API Action: https://docs.aws.amazon.com/step-functions/latest/apireference/API_GetExecutionHistory.html
You will want to look for events of type MapIterationSucceeded, grab the previousEventId, then look for the output in the HistoryEvent with that Id.
For example, if you had a state machine like this:
{
"StartAt": "Generate Array for Map",
"States": {
"Generate Array for Map": {
"Type": "Pass",
"Next": "Map",
"Result": [
1,
2,
3,
4,
5
]
},
"Map": {
"Type": "Map",
"ItemProcessor": {
"ProcessorConfig": {
"Mode": "INLINE"
},
"StartAt": "Generate Random Number",
"States": {
"Generate Random Number": {
"Type": "Pass",
"End": true,
"Parameters": {
"Random Number.$": "States.MathRandom(1,1000)"
}
}
}
},
"End": true
}
}
}
Your execution history would look like this:
{
"events": [
{
"timestamp": "2022-12-08T16:44:07.651000-08:00",
"type": "ExecutionStarted",
"id": 1,
"previousEventId": 0,
"executionStartedEventDetails": {
"input": "{\n \"Comment\": \"Insert your JSON here\"\n}",
"inputDetails": {
"truncated": false
},
"roleArn": "arn:aws:iam::000000000000:role/service-role/StepFunctions-MapTest-role-613230dd"
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateEntered",
"id": 2,
"previousEventId": 0,
"stateEnteredEventDetails": {
"name": "Generate Array for Map",
"input": "{\n \"Comment\": \"Insert your JSON here\"\n}",
"inputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateExited",
"id": 3,
"previousEventId": 2,
"stateExitedEventDetails": {
"name": "Generate Array for Map",
"output": "[1,2,3,4,5]",
"outputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapStateEntered",
"id": 4,
"previousEventId": 3,
"stateEnteredEventDetails": {
"name": "Map",
"input": "[1,2,3,4,5]",
"inputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapStateStarted",
"id": 5,
"previousEventId": 4,
"mapStateStartedEventDetails": {
"length": 5
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapIterationStarted",
"id": 6,
"previousEventId": 5,
"mapIterationStartedEventDetails": {
"name": "Map",
"index": 0
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapIterationStarted",
"id": 7,
"previousEventId": 5,
"mapIterationStartedEventDetails": {
"name": "Map",
"index": 1
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapIterationStarted",
"id": 8,
"previousEventId": 5,
"mapIterationStartedEventDetails": {
"name": "Map",
"index": 2
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapIterationStarted",
"id": 9,
"previousEventId": 5,
"mapIterationStartedEventDetails": {
"name": "Map",
"index": 3
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapIterationStarted",
"id": 10,
"previousEventId": 5,
"mapIterationStartedEventDetails": {
"name": "Map",
"index": 4
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateEntered",
"id": 11,
"previousEventId": 6,
"stateEnteredEventDetails": {
"name": "Generate Random Number",
"input": "1",
"inputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateExited",
"id": 12,
"previousEventId": 11,
"stateExitedEventDetails": {
"name": "Generate Random Number",
"output": "{\"Random Number\":259}",
"outputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapIterationSucceeded",
"id": 13,
"previousEventId": 12,
"mapIterationSucceededEventDetails": {
"name": "Map",
"index": 0
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateEntered",
"id": 14,
"previousEventId": 7,
"stateEnteredEventDetails": {
"name": "Generate Random Number",
"input": "2",
"inputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateExited",
"id": 15,
"previousEventId": 14,
"stateExitedEventDetails": {
"name": "Generate Random Number",
"output": "{\"Random Number\":776}",
"outputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapIterationSucceeded",
"id": 16,
"previousEventId": 15,
"mapIterationSucceededEventDetails": {
"name": "Map",
"index": 1
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateEntered",
"id": 17,
"previousEventId": 8,
"stateEnteredEventDetails": {
"name": "Generate Random Number",
"input": "3",
"inputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateExited",
"id": 18,
"previousEventId": 17,
"stateExitedEventDetails": {
"name": "Generate Random Number",
"output": "{\"Random Number\":743}",
"outputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapIterationSucceeded",
"id": 19,
"previousEventId": 18,
"mapIterationSucceededEventDetails": {
"name": "Map",
"index": 2
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateEntered",
"id": 20,
"previousEventId": 9,
"stateEnteredEventDetails": {
"name": "Generate Random Number",
"input": "4",
"inputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateExited",
"id": 21,
"previousEventId": 20,
"stateExitedEventDetails": {
"name": "Generate Random Number",
"output": "{\"Random Number\":790}",
"outputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapIterationSucceeded",
"id": 22,
"previousEventId": 21,
"mapIterationSucceededEventDetails": {
"name": "Map",
"index": 3
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateEntered",
"id": 23,
"previousEventId": 10,
"stateEnteredEventDetails": {
"name": "Generate Random Number",
"input": "5",
"inputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "PassStateExited",
"id": 24,
"previousEventId": 23,
"stateExitedEventDetails": {
"name": "Generate Random Number",
"output": "{\"Random Number\":632}",
"outputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapIterationSucceeded",
"id": 25,
"previousEventId": 24,
"mapIterationSucceededEventDetails": {
"name": "Map",
"index": 4
}
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapStateSucceeded",
"id": 26,
"previousEventId": 25
},
{
"timestamp": "2022-12-08T16:44:07.698000-08:00",
"type": "MapStateExited",
"id": 27,
"previousEventId": 25,
"stateExitedEventDetails": {
"name": "Map",
"output": "[{\"Random Number\":259},{\"Random Number\":776},{\"Random Number\":743},{\"Random Number\":790},{\"Random Number\":632}]",
"outputDetails": {
"truncated": false
}
}
},
{
"timestamp": "2022-12-08T16:44:07.745000-08:00",
"type": "ExecutionSucceeded",
"id": 28,
"previousEventId": 27,
"executionSucceededEventDetails": {
"output": "[{\"Random Number\":259},{\"Random Number\":776},{\"Random Number\":743},{\"Random Number\":790},{\"Random Number\":632}]",
"outputDetails": {
"truncated": false
}
}
}
]
}
I have this List:
var listMap = [
{
"type": "Spouse",
"name": "Mother",
"age": 50
},
{
"type": "Family",
"name": "Lili",
"age": 16
},
];
var finalResult = [
"type": "Spouse",
"name": "Mother",
"age": 50,
"type": "Family",
"name": "Lili",
"age": 16, ];
How can i achieve finalResult?
List<Map<String,dynamic>> listMap = [
{
"type": "Spouse",
"name": "Mother",
"age": 50
},
{
"type": "Family",
"name": "Lili",
"age": 16
},
];
try to loop them and add them 1 by 1
for(var x in listMap){
for(var y in x.entries){
newList.add("${jsonEncode(y.key)}:${jsonEncode(y.value)}");
}
}
// or use print
log(newList.toList().toString());
result
["type":"Spouse", "name":"Mother", "age":50, "type":"Family", "name":"Lili", "age":16]
you can try this to dart pad.
Getting the error RangeError (end): Invalid value: Only valid value is 0: 1 when generating model when I try to generate model
Expected Behavior
when I use this command get generate model on match_details with assets/models/to_get/match_details.json
it should be creating a model inside the match_details folder with the fields inside the JSON file mentioned.
Current Behavior
When I try to do this I get the error RangeError (end): Invalid value: Only valid value is 0: 1 when generating model** when I try to generate model
Possible Solution
Am sure it is something about parsing the JSON file
More info
here is what the JSON file looks like
{
"success": true,
"details": {
"id": 22,
"date_time": "2022-04-12T20:00:00Z",
"home_team": {
"id": 69,
"home_strip": {
"id": 15,
"image": "/uploads/strip_5_0py3QXM.png"
},
"away_strip": {
"id": 16,
"image": "/uploads/WhatsApp_Image_2022-03-09_at_10.24.56_AM.jpeg"
},
"players": [{
"id": 47,
"is_cap": false,
"average_skill": 3,
"region": {
"id": 13,
"name_ar": "التجمع الخامس",
"name_en": "Fifth Settlement",
"state": {
"id": 8,
"name_ar": "القاهره",
"name_en": "Cairo"
}
},
"first_name": "Ezz El Din",
"last_name": "Karim",
"mobile": "01113481110",
"email": null,
"image": null,
"positions_ar": [{
"مدافع": 3
}],
"positions_en": [{
"CB": 3
}],
"basic_skills_ar": [{
"السرعة": 4
},
{
"تمرير الكرة": 3
},
{
"تسديد": 2
}
],
"basic_skills_en": [{
"speeding": 4
},
{
"Passing": 3
},
{
"shooting": 2
}
]
}],
"cap": {
"id": 46,
"is_cap": true,
"average_skill": 3,
"region": {
"id": 13,
"name_ar": "التجمع الخامس",
"name_en": "Fifth Settlement",
"state": {
"id": 8,
"name_ar": "القاهره",
"name_en": "Cairo"
}
},
"first_name": "Karim",
"last_name": "Garrana",
"mobile": "01113332257",
"email": "karim.garrana#gmail.com",
"image": null,
"positions_ar": [{
"مهاجم": 3
},
{
"مدافع": 4
}
],
"positions_en": [{
"ST": 3
},
{
"CB": 4
}
],
"basic_skills_ar": [{
"السرعة": 3
},
{
"تمرير الكرة": 3
},
{
"تسديد": 3
}
],
"basic_skills_en": [{
"speeding": 3
},
{
"Passing": 3
},
{
"shooting": 3
}
]
},
"average_skill": 3,
"points": 0,
"name": "كوبرا",
"league": 25,
"logo": "/uploads/9_IApeihR.png",
"region_ar": "التجمع الخامس",
"region_en": "Fifth Settlement"
},
"away_team": {
"id": 68,
"home_strip": {
"id": 14,
"image": "/uploads/strip_6.png"
},
"away_strip": {
"id": 13,
"image": "/uploads/strip_5.png"
},
"players": [{
"id": 46,
"is_cap": true,
"average_skill": 3,
"region": {
"id": 13,
"name_ar": "التجمع الخامس",
"name_en": "Fifth Settlement",
"state": {
"id": 8,
"name_ar": "القاهره",
"name_en": "Cairo"
}
},
"first_name": "Karim",
"last_name": "Garrana",
"mobile": "01113332257",
"email": "karim.garrana#gmail.com",
"image": null,
"positions_ar": [{
"مهاجم": 3
},
{
"مدافع": 4
}
],
"positions_en": [{
"ST": 3
},
{
"CB": 4
}
],
"basic_skills_ar": [{
"السرعة": 3
},
{
"تمرير الكرة": 3
},
{
"تسديد": 3
}
],
"basic_skills_en": [{
"speeding": 3
},
{
"Passing": 3
},
{
"shooting": 3
}
]
}],
"cap": {
"id": 45,
"is_cap": false,
"average_skill": 4,
"region": {
"id": 11,
"name_ar": "ميامى",
"name_en": "Miami",
"state": {
"id": 7,
"name_ar": "الإسكندرية",
"name_en": "Alexandria"
}
},
"first_name": "Ahmed",
"last_name": "Wagdi",
"mobile": "01207199086",
"email": "ahmed.w.amin#gmail.com",
"image": "/uploads/90f1e1c9-b430-4f9c-8347-a86be57f58676954964268664537546.jpg",
"positions_ar": [{
"مدافع": 5
}],
"positions_en": [{
"CB": 5
}],
"basic_skills_ar": [{
"تسديد": 5
},
{
"تمرير الكرة": 5
},
{
"السرعة": 5
}
],
"basic_skills_en": [{
"shooting": 5
},
{
"Passing": 5
},
{
"speeding": 5
}
]
},
"average_skill": 4,
"points": 2,
"name": "المنتقمون",
"league": 25,
"logo": "/uploads/6.png",
"region_ar": "التجمع الخامس",
"region_en": "Fifth Settlement"
},
"location": {
"id": 8,
"name_ar": "مدرسة الكابيتال",
"name_en": "Capital School",
"location": "30.014650515430546,31.4582347869873",
"region": 13
}
}
}
I have an API that returns a response similar to the one shown in this post.
My response comes back with objects in the list only containing a $ref variable that is pointing to a child object somewhere in the list with a relating $id.
I tried the following but it would not work for larger more complicated responses and I suspect that it would increase the compute time a lot:
Future<List<Category>> getCategories() async {
String token = await FlutterSession().get("accessToken");
final response =
await http.get('$url/api/categories/', headers: <String, String>{
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + token,
});
if (response.statusCode == 200) {
categories = categoriesFromJson(response.body);
var incompleteCats = categories.where((c) => c.id == null);
incompleteCats.forEach((ic) {
categories.remove(ic);
var completeCat =
categories.where((s) => s.parent?.$id == ic.$ref).first.parent;
categories.add(completeCat);
});
return categories;
} else {
throw Exception('Failed to get categories');
}
}
Example of API Response
[
{
"$id": "3",
"Parent": {
"$id": "4",
"Parent": {
"$ref": "1"
},
"CategoryName": "General",
"Description": "Test",
"ParentCategoryId": 45,
"SortOrder": 2,
"Id": 49,
"IsDeleted": false,
"CreatedDate": "2009-11-29T00:00:00",
"CreatedByUserId": 1,
"UpdatedDate": "1900-01-01T00:00:00",
"UpdatedByUserId": null,
"State": 0
},
"CategoryName": "Country",
"Description": "Test",
"ParentCategoryId": 49,
"SortOrder": 3,
"Id": 47,
"IsDeleted": false,
"CreatedDate": "2019-11-29T00:00:00",
"CreatedByUserId": 1,
"UpdatedDate": "1900-01-01T00:00:00",
"UpdatedByUserId": null,
"State": 0
},
{
"$id": "16",
"Parent": {
"$ref": "4"
},
"CategoryName": "Other",
"Description": "Other",
"ParentCategoryId": 49,
"SortOrder": 7,
"Id": 61,
"IsDeleted": false,
"CreatedDate": "2020-06-05T09:13:50.863",
"CreatedByUserId": 1,
"UpdatedDate": "2020-06-05T09:13:50.863",
"UpdatedByUserId": 1,
"State": 0
},
{
"$ref": "4"
},
]