JSON > CSV: Cannot read properties of undefined (reading 'map')

Describe the problem/error/question

I intend to convert json data into a csv file.

the input data is from an http-request from the twitter API. I also want the data edited to my needs. examples below.

unfortunately the dictionary/mapping I am using is throwing the error messages below.
I already had the script running in my VS Code environment, but since n8n has a different environment, it wasnt 100% applicable.

What is the error message (if any)?

Cannot read properties of undefined (reading ‘map’) [line 18]

Element implicitly has an ‘any’ type because expression of type ‘any’ can’t be used to index type ‘{}’.

the code in the code node:

const csvjson = require('csvjson'); //costum container with this module used

// Reading JSON data from prev Output
var fileContent = $input.all();

const jsonData = typeof fileContent === 'string' ? JSON.parse(fileContent) : fileContent;

// Creating a map of media_key to type
const mediaMap = {};
if (jsonData.includes && jsonData.includes.media) {
    jsonData.includes.media.forEach(media => {
        mediaMap[media.media_key] = media.type; //error happening here and...
    });
}

const dataOnly = jsonData.data.map(item => {
    const result = {
        text: item.text,
        tweet_id: item.id
    };
    
    // If attachments exist, get the type from the media map
    if (item.attachments && item.attachments.media_keys && item.attachments.media_keys.length > 0) {
        const mediaKey = item.attachments.media_keys[0];
        result.attachment_type = mediaMap[mediaKey] || 'unknown'; //...and probably error here
    }
    
    return result;
});

// Converting JSON to CSV
const csvData = csvjson.toCSV(JSON.stringify(dataOnly), {
    headers: 'key'
});

console.log('Conversion successful. CSV file created.');

input and output data:

input data that I got from the API request (data anonymised):

{
    "data":
    [
        {"created_at":"2025-12-10T18:32:03.000Z","id":"123456789","attachments":{"media_keys":["13_1234567890"]},"text":"textcontent1","edit_history_tweet_ids":["123456789"]},
        {"created_at":"2025-12-09T17:22:36.000Z","id":"987654321","attachments":{"media_keys":["7_987654321"]},"text":"textcontent2","edit_history_tweet_ids":["987654321"]}
    ],
    "includes":
        {
            "media":
            [
                {"media_key":"13_1234567890","type":"video"},
                {"media_key":"7_987654321","type":"video"}
            ]
        },
    "meta":
    {
        "next_token":"afilu134hf02adfhi8","result_count":2,"newest_id":"123456789","oldest_id":"987654321"
    }
}

what the output is supposed to look like:

text,tweet_id,attachment_type
textcontent1,123456789,video
textcontent2,987654321,video

Information on your n8n setup

  • n8n version: 1.123.5
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker Desktop, self hosted
  • Operating system: WIn10

This is how I would go about this.
But one thing to consider:
There could be more than one attachment theoretically. How do you want to process that?
Right now, my code only adds the first one. Do you want multiple rows with the same tweetId, or multiple attachment_types with a separator in one row?

1 Like

thanks for helping salman,
guess I overcomplicated it a bit.

the mapping still showed an error when using the API data, but after analising the dfferences between your mock data and my API data, I noticed the imported curl command still had the -o parameter, which caused the raw data to be empty, because its inside a file.

regarding multiple attachements, I hadnt considered that yet, so thanks for mentioning it. I ll cross that bridge, when I get to it, but multiple rows are probably cleaner than more columns.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.