Created
September 3, 2022 13:15
-
-
Save lisilinhart/f3e0e2883868ba869e1cb9142d18a0b7 to your computer and use it in GitHub Desktop.
Transform JSON data for Storyblok
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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