Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created June 16, 2020 09:16
Show Gist options
  • Save hubgit/712472260c0fd68bb3e10e434feccd0f to your computer and use it in GitHub Desktop.
Save hubgit/712472260c0fd68bb3e10e434feccd0f 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 })
}
@jaitjacob
Copy link

thanks for this. was struggling with NodeList manipulation this gave me a direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment