Home > Back-end >  needs to added json parent array name to every sub array
needs to added json parent array name to every sub array

Time:10-01

Flutter / Dart Question

This is the array which I have , In the Accessories list it contains brand name("samsung"), I need to added the brand name to all the sub array.

you can see the second code space in the sub array of Brand name key it contains brand name of the parent key

{
    "Accessories": [
        {"id": 1,
         "brand": "samsung",
         "parentId": null,
         "children": [
                {
                    "id": 4,
                    "name": "Ace",
                    "parentId": 1
                },
                {
                    "id": 5,
                    "name": "note",
                    "parentId": 1
                },
                {
                    "id": 6,
                    "name": "galaxy",
                    "parentId": 1
                }
            ]
        },
        {"id": 2,
          "name": "Asus",
          "parentId": null,
          "children": [
                {
                    "id": 7,
                    "name": "gaming",
                    "parentId": 2
                },
                {
                    "id": 8,
                    "name": "office",
                    "parentId": 2
                }
            ]
        },
        
    ]
}

what I expecting result is below

{
    "phones": [
        {
            "id": 1,
            "brand": "samsung",
            "parentId": null,
            "children": [
                {
                    "id": 4,
                    "name": "Ace",
                    *"brand": "samsung",*
                    "parentId": 1
                },
                {
                    "id": 5,
                    "name": "note",
                    *"brand": "samsung",*
                    "parentId": 1
                },
                {
                    "id": 6,
                    "name": "galaxy",
                    *"brand": "samsung",*
                    "parentId": 1
                }
            ]
        },
        {
            "id": 2,
            "name": "Asus",
            "parentId": null,
            "children": [
                {
                    "id": 7,
                    "name": "gaming",
                    *"name": "Asus",*
                    "parentId": 2
                },
                {
                    "id": 8,
                    "name": "office",
                    *"name": "Asus",*
                    "parentId": 2
                }
            ]
        }, 
    ]
}

please help me to resolve the question . ..............................................................................................................................

CodePudding user response:

Assuming you are not using any class to encode and decode json and you just want to get from the first json string to the second json string:

  Map<String, dynamic> decodedMap = json.decode(accessoriesJson);
  final List accessories = decodedMap['Accessories'];
  final List result = accessories.map((a) {
    return {
      ...a,
      'children': (a['children'] as List)
          .map((c) => ({...c, 'parent': a['brand']}))
          .toList(),
    };
  }).toList();

  final resultJson = json.encode({
    'phones': result,
  });

Note: I assumed the name of the parent on the first json is called brand (Like in the samsung example). Although in the Asus example it's called name.

CodePudding user response:

You can create json decoder/encoder class with https://app.quicktype.io/ I used the same to convert the json to Accessories type. After that you can use the forEach two times to update the existing array without creating a new one.

Like this

  var list = Accessories.fromMap(json);
  print(list.accessories);
  list.accessories!.forEach((e) {
    var brand = e.brand;
    if (brand != null) {
      e.children?.forEach((element) {
        element.brand = brand;
      });
    }
  });

  print(list.toMap());

I am only using "brand" because

  1. You should keep your property name same if using array.
  2. Using "name" in parent will add property "name" in child. Which is wrong because there is already a property named "name" in child. So it will replace the child name.

Working example

https://dartpad.dev/?id=a7f4a869d02db3b929e3814db28fcbe1&null_safety=true

I had to make changes for null handling in type class as I don't know what will be the optional in this array. So I made everything optional.

  • Related