Home > Software design >  Flutter: parsing json response that has multiple keys
Flutter: parsing json response that has multiple keys

Time:09-21

I'm trying to get the values of these keys from this json response:

{
    "pro": {
        "groups": [
            "1": {
                "name": "Base",
                "fields": [
                    {
                        "id": 3,
                        "value": {
                            "raw": "Name",
                        }
                    },
                    {
                        "id": 4,
                        "value": {
                            "raw": "avatar",
                        }
                    },
                ]
            },
            "2": {
                "name": "Base",
                "fields": [
                    {
                        "id": 6,
                        "value": {
                            "raw": "Name",
                        }
                    },
                    {
                        "id": 7,
                        "value": {
                            "raw": "avatar",
                        }
                    },
                ]
            }
    ]
}
}        
        

I could get the values "name": "Base"

json['pro']['groups']["1"]['name'],

But I can't get the values of key "raw".

How can I get the values of key "raw"?

CodePudding user response:

The values of fields are a list, so you will get a list of raw values:

List<String> raw = json['pro']['groups']['1']['fields'].map((v) => v['value']['raw'];

Also, it seems like groups is an array but as an object? then you can do something like this:

List<String> raw = [];
Map<String, dynamic> groups = json['pro']['groups'];
for (var key in groups.keys) {
  raw.add(groups[key]['fields'].map((v) => v['value']['raw']);
}

or

List<String> raw = groups.keys.map((key) => groups[key]['fields'].map((v) => v['value']['raw']);

I haven't tested the code, but hopefully, it works as expected :)

CodePudding user response:

first thing first. your json is invalid. try to paste your json here it will show why is your json is invalid

after you fix the json the new structure json will be looked like this

{
  "pro": {
    "groups": [
      {
        "name": "Base",
        "fields": [
          {
            "id": 3,
            "value": {
              "raw": "Name",
            }
          },
          {
            "id": 4,
            "value": {
              "raw": "avatar",
            }
          },
        ]
      },
      {
        "name": "Base",
        "fields": [
          {
            "id": 6,
            "value": {
              "raw": "Name",
            }
          },
          {
            "id": 7,
            "value": {
              "raw": "avatar",
            }
          },
        ]
      }
    ]
  }
}

and then in order to grab the value of raw, you have to parse the json first using jsonDecode(), then you can use something like this:

Map<String, dynamic> groupOne = json['pro']['groups'][0];
Map<String, dynamic> groupOneFieldOne = groupOne['fields'][0];
print(groupOneFieldOne['value']['raw']);

but that's just an example. if you want to access them easily you can use .map() like this:

  List<Map<String, dynamic>> groups = json['pro']['groups'];
  groups.map(
    (Map<String, dynamic> group) => (group['fields'] as List<dynamic>).map(
      (dynamic field) => field['value']['raw'],
    ),
  );

that's it! if you want to ask anything just put a comment ;) you can copy and paste on dartpad

  List<Map<String, dynamic>> groups = json['pro']['groups'];
  print(groups.map(
    (Map<String, dynamic> group) => (group['fields'] as List<dynamic>).map(
      (dynamic field) => field['value']['raw'],
    ),
  ));
}

Map<String, dynamic> json = {
  "pro": {
    "groups": [
      {
        "name": "Base",
        "fields": [
          {
            "id": 3,
            "value": {
              "raw": "Name",
            }
          },
          {
            "id": 4,
            "value": {
              "raw": "avatar",
            }
          },
        ]
      },
      {
        "name": "Base",
        "fields": [
          {
            "id": 6,
            "value": {
              "raw": "Name",
            }
          },
          {
            "id": 7,
            "value": {
              "raw": "avatar",
            }
          },
        ]
      }
    ]
  }
};
  • Related