How to migrate data to Contentful


Advanced

Here’s a rough example how to migrate data from json file to Contentful using JavaScript (Node.js) and Contentful Management SDK.

You basicly have some array of data in json file and you map your data to Contentful entry structure you have in your Contentful datamodel (example in const entry). I assume you have some featured image or similar so when we create Contentful entry we first upload related image files and create a Contentful asset out of those. Then we get that asset id to be added to our actual entry as teaser image. Then just create entry to your chosen entry type in Contentful and at last publish it.

const contentful = require('contentful-management');
let rawData = require('../data/data_from_sql.json');

export default async function createContentfulEntry(data)  {


    const entry = (url) => {
        const fileName = url.split('/').pop();
        const fileExt = fileName.split('.').pop();
        const contentType = `image/${fileExt}`;
    
        const client = contentful.createClient({
            accessToken: "ACCESS_TOKEN_HERE"
          })
      
        const res = client.getSpace('SPACE_ID_HERE')
          .then((space) => space.getEnvironment('ENVIRONMENT_NAME_HERE'))
          .then((environment) => environment.createAsset({
            fields: {
              title: {
                'fi': fileName
              },
              description: {
                'fi': ''
              },
              file: {
                'fi': {
                  contentType: contentType,
                  fileName: fileName,
                  upload: url
                }
              }
            }
          }))
          .then((asset) => asset.processForAllLocales())
          .then((asset) => asset.publish())
          .then((asset) => {
              const assetid = asset.sys.id;

             data.contentful.fields.teaserImage.en.sys.id = assetid;
             return client.getSpace('SPACE_ID_HERE')
            }
          )
          .then((space) => space.getEnvironment('ENVIRONMENT_NAME_HERE'))
          .then((environment) => environment.createEntry('ENTRY_TYPE_HERE', JSON.stringify(data.contentful)))
          .then((entry) => entry.publish())
          .then((entry) => {
              return entry;
          })
          .catch(console.error)

   
        return res;
    
    }


    return await entry(data.imgurl);
}


const migrateDummyData = () => {
    rawData.forEach(data => {
        const entry = {
            imgurl: data.imgurl,
            contentful: {
              fields: {
                title: {
                  en: data.title
                },
                slug: {
                  en: data.slug
                },
                teaserImage: {
                  en: {
                    sys: {
                      id: '',
                      linkType: 'Asset',
                      type: 'Link'
                    }
                  }  
                },
                content: {
                  en: data.content
                },
              }
            }   
          }
          createContentfulEntry(entry);
    });
    
}

migrateDummyData();