Skip to content

Instantly share code, notes, and snippets.

@lisilinhart
Created September 3, 2022 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lisilinhart/f3e0e2883868ba869e1cb9142d18a0b7 to your computer and use it in GitHub Desktop.
Save lisilinhart/f3e0e2883868ba869e1cb9142d18a0b7 to your computer and use it in GitHub Desktop.
Transform JSON data for Storyblok
const fs = require('fs')
const StoryblokClient = require('storyblok-js-client')
const { v4 } = require('uuid')
const Storyblok = new StoryblokClient({
oauthToken: 'YOUR_PERSONAL_ACCESS_TOKEN' // can be found in your My account section
})
const config = {
jsonFile: './speaking.json',
spaceId: 'SPACE_ID',
storyId: 'STORY_ID',
}
async function readStory() {
const { data } = await Storyblok.get(`spaces/${config.spaceId}/stories/${config.storyId}`)
// console.log("new structure", data.story.content.body[0])
return data.story
}
async function transformData() {
const file = fs.readFileSync(config.jsonFile)
const speakingEvents = JSON.parse(file);
// console.log("old structure", speakingEvents[0])
const newEvents = speakingEvents.map(event => {
return {
_uid: v4(),
date: event.date + ' 12:00',
website: {
id: '',
url: event.link,
linktype: 'url',
fieldtype: 'multilink',
cached_url: event.link,
},
location: event.location,
component: 'Event',
talk_name: event.title,
event_name: event.conference,
event_type: event.category,
slide_link: event?.slides || '',
video_link: event?.video || '',
}
})
// console.log("transformed structure", newEvents[0])
return newEvents
}
async function main() {
const story = await readStory()
const newData = await transformData()
story.content.body = newData
Storyblok.put(`spaces/${config.spaceId}/stories/${config.storyId}`, {
"story": story,
"force_update": 1,
"publish": 1
})
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment