getj
v0.0.12
Published
get json
Downloads
4
Readme
getj
getj(uri: string): Promise<Object>
The getj
function is an asynchronous function that retrieves JSON-LD or JSON data from a given URI.
Parameters
uri
(string): The URI from which to fetch the JSON-LD or JSON data.
Returns
- A Promise that resolves to the JSON-LD or JSON object found in the fetched document.
Usage
import getj from 'getj'
getj('https://example.com')
.then(json => {
console.log(json)
})
.catch(error => {
console.error('Error:', error)
})
Demo
Description
The getj
function takes a URI as input and performs the following steps:
- Fetches the document at the given URI.
- Parses the fetched document into a DOM structure.
- Searches for a
<script>
element with atype
attribute of "application/ld+json" or "application/json". - If found, parses the content of the
<script>
element into a JSON object. - If a fragment identifier is present in the input URI, searches for a nested object with a matching ID or '@id' property within the JSON object.
- Returns the matching nested object if found, or the entire JSON object if no fragment identifier is present or no matching object is found.