Skip to content

Instantly share code, notes, and snippets.

@hubgit
Last active June 16, 2020 09:40
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 hubgit/9ef9e56291a3af5d7e719befb61f7062 to your computer and use it in GitHub Desktop.
Save hubgit/9ef9e56291a3af5d7e719befb61f7062 to your computer and use it in GitHub Desktop.
Fetch, extract, parse, expand, frame and compact JSON-LD
const { JSDOM } = require('jsdom')
const { compact, expand, frame } = require('jsonld')
const url = 'https://www.bbc.co.uk/schedules/p00fzl6p/2020/06/14'
// fetch and parse HTML
const { window: { document } } = await JSDOM.fromURL(url)
// select the script elements containing JSON-LD
const elements = document.querySelectorAll('script[type="application/ld+json"]')
for (const element of elements) {
// parse the JSON
const doc = JSON.parse(element.textContent)
console.log({ doc })
// expand the doc to use full URIs for property names
// const expanded = await expand(doc)
// console.log({ expanded })
// frame the doc to filter and pick certain properties
const framed = await frame(doc, {
'@context': 'https://schema.org/',
'@type': 'TVEpisode',
'@explicit': true,
identifier: {},
description: {},
name: {},
published: {},
image: {},
partOfSeries: {
'@type': 'TVSeries',
'@explicit': true,
'@default': [],
identifier: {},
name: {},
},
partOfSeason: {
'@type': 'TVSeason',
'@explicit': true,
'@default': [],
identifier: {},
name: {},
position: {},
},
})
console.log({ framed })
// compact the framed doc to use own property names
const compacted = await compact(framed, {
'@context': {
identifier: 'http://schema.org/identifier',
description: 'http://schema.org/description',
name: 'http://schema.org/name',
series: 'http://schema.org/partOfSeries',
season: 'http://schema.org/partOfSeason',
position: 'http://schema.org/position',
image: {
'@type': '@id',
'@id': 'http://schema.org/image',
},
published: {
'@type': 'http://schema.org/Date',
'@id': 'http://schema.org/datePublished',
},
},
})
console.log({ compacted })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment