Skip to content

Instantly share code, notes, and snippets.

@HoverBaum
Last active March 4, 2020 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HoverBaum/59ff147998052cf26b745be25299a82f to your computer and use it in GitHub Desktop.
Save HoverBaum/59ff147998052cf26b745be25299a82f to your computer and use it in GitHub Desktop.
Filter empty assets from Contentful export.

Using the Contentful Export tool I noticed that you can currently run into a situation where you export assets that have no files linked which will through an error when you try to pass the exported json into the Contentful Import tool as discussed in issue 95.

As a temporary solution I wrote myselve this script that you can pipe data through to filter out these malformed assets.

cat export.json | node filter-empty-assets.js > filtered-export.json

Now you can use the filteres-export.json for your import and it should work.

#!/usr/bin/env node
const stdin = process.stdin
const stdout = process.stdout
const inputChunks = []
stdin.resume()
stdin.setEncoding('utf8')
stdin.on('data', function (chunk) {
inputChunks.push(chunk)
})
stdin.on('end', function () {
const inputJSON = inputChunks.join('')
const parsedData = JSON.parse(inputJSON)
const filteredData = {
...parsedData,
assets: parsedData.assets.filter(asset => asset.fields.file)
}
const outputJSON = JSON.stringify(filteredData, null, 2)
stdout.write(outputJSON)
stdout.write('\n')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment