node fetching json from url



I am trying to fetch and parse the JSON returned by the below, in Nodejs. It seems the problem is that the node code is trying to parse the JSON string which contains newline characters, but I am not sure how to avoid this, and why fetch works in chrome devtools.

const url = ""

I can successfully run this in the chrome devtools open on the domain. Also, the JSON appears to automatically get parsed to an object, which I didn't expect.

fetch(url).then(res => res.json()).then(json => console.log(json.pageItems))

I have tried the following in node, but none work.

const fetch = require("node-fetch");
await fetch(url).then(res => res.json()); // Unexpected token in JSON at position 0

const rp = require('request-promise-native');
const json = await rp({uri: url}) // returns JSON string with newline characters
JSON.parse(json) // Unexpected token in JSON at position 0

Continue reading...