Adding separate tweet data from JSON onto a List<String>

Question

Asked by Adriana on November 14, 2021 (source).

I've taken the following JSON example directly from Twitter's API example using Postman, my question is how would I be able to grab each tweets inside the "data" section and add each individual tweet onto a List < String > so that each individual tweet's sub JSON is saved as String in the List. Would this be possible? I was attempting to use the JSON parsing method decoding and encoding from Dart but that did not work, I'm not sure if its because the JSON example features the "data" section and the "errors" section. Any help would be greatly appreciated, thank you!

Example JSON:

{
  "data": [
    {
      "author_id": "12",
      "conversation_id": "20",
      "created_at": "2006-03-21T20:50:14.000Z",
      "id": "20",
      "text": "just setting up my twttr"
    },
    {
      "attachments": {
        "media_keys": [
          "16_1276500934466703361"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1275244210439028736",
      "created_at": "2020-06-23T01:48:07.000Z",
      "entities": {
        "urls": [
          {
            "start": 112,
            "end": 135,
            "url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
            "display_url": "pic.twitter.com/dpI2lRmj9F"
          }
        ]
      },
      "id": "1275244210439028736",
      "text": "Need to follow what’s happening in real time? Change your timeline to show latest Tweets instead of top Tweets."
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274087263073255425"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:32.000Z",
      "entities": {
        "mentions": [
          {
            "start": 13,
            "end": 22,
            "username": "YoliZama"
          }
        ],
        "urls": [
          {
            "start": 23,
            "end": 46,
            "url": "https://twitter.com/Twitter/status/1274087695145332736/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1274087695145332736/photo/1",
            "display_url": "pic.twitter.com/lcGDLzAJIn"
          }
        ]
      },
      "id": "1274087695145332736",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1274087694105075714"
        }
      ],
      "text": "📍 Oakland\n🗣️ @YoliZama"
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274086977952833536"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:32.000Z",
      "entities": {
        "mentions": [
          {
            "start": 19,
            "end": 31,
            "username": "Afrikkana95"
          }
        ],
        "urls": [
          {
            "start": 32,
            "end": 55,
            "url": "https://twitter.com/Twitter/status/1274087694105075714/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1274087694105075714/photo/1",
            "display_url": "pic.twitter.com/tEfs27p7xu"
          }
        ]
      },
      "id": "1274087694105075714",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1274087692003770368"
        }
      ],
      "text": "📍 New York City\n🗣️ @Afrikkana95"
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274086862907305984"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:31.000Z",
      "entities": {
        "mentions": [
          {
            "start": 13,
            "end": 25,
            "username": "JoshuaKissi"
          }
        ],
        "urls": [
          {
            "start": 26,
            "end": 49,
            "url": "https://twitter.com/Twitter/status/1274087692003770368/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1274087692003770368/photo/1",
            "display_url": "pic.twitter.com/ZeD3XvJUbX"
          }
        ]
      },
      "id": "1274087692003770368",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1274087690758090752"
        }
      ],
      "text": "📍 Chicago\n🗣️ @JoshuaKissi"
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274086703272038401"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:31.000Z",
      "entities": {
        "mentions": [
          {
            "start": 18,
            "end": 33,
            "username": "Imani_Barbarin"
          }
        ],
        "urls": [
          {
            "start": 34,
            "end": 57,
            "url": "https://twitter.com/Twitter/status/1274087690758090752/photo/1",
            "display_url": "pic.twitter.com/ZRDUipsu38",
            "expanded_url": "https://twitter.com/Twitter/status/1274087690758090752/photo/1",
            "display_url": "pic.twitter.com/ZRDUipsu38"
          }
        ]
      },
      "id": "1274087690758090752",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1274087689487134720"
        }
      ],
      "text": "📍 Philadelphia\n🗣️ @Imani_Barbarin "
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274086530919718917"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:30.000Z",
      "entities": {
        "mentions": [
          {
            "start": 13,
            "end": 25,
            "username": "BerniceKing"
          }
        ],
        "urls": [
          {
            "start": 26,
            "end": 49,
            "url": "https://twitter.com/Twitter/status/1274087688321200128/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1274087688321200128/photo/1",
            "display_url": "pic.twitter.com/83upyVnwIS"
          }
        ]
      },
      "id": "1274087688321200128",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1274087687469715457"
        }
      ],
      "text": "📍 Atlanta\n🗣️ @BerniceKing "
    },
    {
      "attachments": {
        "media_keys": [
          "3_1274086027544498176"
        ]
      },
      "author_id": "783214",
      "conversation_id": "1274087687469715457",
      "created_at": "2020-06-19T21:12:30.000Z",
      "entities": {
        "mentions": [
          {
            "start": 17,
            "end": 29,
            "username": "FredTJoseph"
          }
        ],
        "urls": [
          {
            "start": 30,
            "end": 53,
            "url": "https://twitter.com/Twitter/status/1274087687469715457/photo/1",
            "expanded_url": "https://twitter.com/Twitter/status/1274087687469715457/photo/1",
            "display_url": "pic.twitter.com/lNTOkyguG1"
          }
        ]
      },
      "id": "1274087687469715457",
      "text": "📍 Minneapolis\n🗣️ @FredTJoseph"
    },
    {
      "author_id": "783214",
      "conversation_id": "1274034244700930049",
      "created_at": "2020-06-19T17:40:09.000Z",
      "entities": {
        "hashtags": [
          {
            "start": 106,
            "end": 115,
            "tag": "BlackJoy"
          }
        ],
        "mentions": [
          {
            "start": 3,
            "end": 14,
            "username": "Blackbirds"
          }
        ]
      },
      "id": "1274034244700930049",
      "referenced_tweets": [
        {
          "type": "retweeted",
          "id": "1274014870707437570"
        }
      ],
      "text": "RT @Blackbirds: Juneteenth is a celebration. It’s about our freedom. And within that freedom is our joy.\n\n#BlackJoy is a form of resistance…"
    },
    {
      "author_id": "773578328498372608",
      "conversation_id": "1275473478779469825",
      "created_at": "2020-06-23T16:59:09.000Z",
      "entities": {
        "mentions": [
          {
            "start": 3,
            "end": 10,
            "username": "Policy"
          }
        ]
      },
      "id": "1275473478779469825",
      "referenced_tweets": [
        {
          "type": "retweeted",
          "id": "1275192966953476100"
        }
      ],
      "text": "RT @Policy: Statement on US high-skilled immigration proclamation: \n\n\"This proclamation undermines America’s greatest economic asset: its d…"
    }
  ],
  "errors": [
    {
      "detail": "Could not find tweet with ids: [1276230436478386177].",
      "title": "Not Found Error",
      "resource_type": "tweet",
      "parameter": "ids",
      "value": "1276230436478386177",
      "type": "https://api.twitter.com/2/problems/resource-not-found"
    }
  ]
}

Output: List < String > where one element in the list could look like this:

{
  "attachments": {
    "media_keys": [
      "16_1276500934466703361"
    ]
  },
  "author_id": "783214",
  "conversation_id": "1275244210439028736",
  "created_at": "2020-06-23T01:48:07.000Z",
  "entities": {
    "urls": [
      {
        "start": 112,
        "end": 135,
        "url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
        "expanded_url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
        "display_url": "pic.twitter.com/dpI2lRmj9F"
      }
    ]
  },
  "id": "1275244210439028736",
  "text": "Need to follow what’s happening in real time? Change your timeline to show latest Tweets instead of top Tweets."
},

It would essentially grab one whole tweet's subJson and save it onto the List. This goes hand in hand with an issue I was facing a few days back concerning this problem , but I think the problem is that the JSON response I get back using the HTTP library is not able to be added properly onto a List.

Answer

Question answered by Dorrineeee (source).

List<Map<String, dynamic>> jsonRes =
    (inputMap['data'] as List<Map>).map((t) => {
        if (t.containsKey('attachments')) 'attachments': t['attachments'],
        if (t.containsKey('author_id')) 'author_id': t['author_id'],
        if (t.containsKey('conversation_id')) 'conversation_id': t['conversation_id'],
        if (t.containsKey('created_at')) 'created_at': t['created_at'],
        if (t.containsKey('entities')) 'entities': t['entities'],
        if (t.containsKey('id')) 'id': t['id'],
        if (t.containsKey('text')) 'text': t['text'],
    }).toList();

List<String> stringRes = jsonRes.map((e) => jsonEncode(e)).toList();

Alternatively:

final List<String> wantedKeys = [
    'attachments',
    'author_id',
    'conversation_id',
    'created_at',
    'entities',
    'id',
    'text'
];
List<Map<String, dynamic>> jsonRes = (inputMap['data']
        as List<Map<String, dynamic>>)
    .map((m) => Map.fromEntries(m.entries.where((e) => wantedKeys.contains(e.key))))
    .toList();

List<String> stringRes = jsonRes.map((e) => jsonEncode(e)).toList();
DART FLUTTER JSON
SHARE: