admin管理员组文章数量:1405118
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. I'm guessing the problem is my simplistic knowledge of https requests, so I would really appreciate it if somebody can explain what is going wrong.
const url = "=&pageNumber=1&contextId=1126&onlyShow=&&&&&&&"
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
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. I'm guessing the problem is my simplistic knowledge of https requests, so I would really appreciate it if somebody can explain what is going wrong.
const url = "https://beta.charitymission.gov.uk/umbraco/api/charityApi/getSearchResults?searchText=&pageNumber=1&contextId=1126&onlyShow=&&&&&&&"
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
Share
Improve this question
edited Nov 8, 2019 at 22:24
Max888
asked Nov 8, 2019 at 22:08
Max888Max888
3,8505 gold badges35 silver badges76 bronze badges
2 Answers
Reset to default 3The problem is that your JSON file is saved in UTF-8 BOM format.
What does it mean?
Your file begins with a so-called Byte Order Mark character, U+FEFF
(Zero Width No-break Space).
That's invisible (has zero width), but still there, and the JSON interpreter fails to parse it.
How to solve?
- Save the JSON data without BOM, or
Use
res.text()
and thenJSON.parse(text.slice(1))
to remove the leading character:fetch(url) .then(res => res.text()) .then(text => JSON.parse(text.slice(1))) .then(json => console.log(json.pageItems))
Why does it work in Chrome?
Chrome automatically removes the BOM character from any fetched/downloaded file, to avoid similar issues.
Try using the default fetch
:
const json = await fetch(url).then(res => res.json())
res.json()
parses the JSON data and returns an object. Use await
to return the data into the variable json
.
本文标签: javascriptnode fetching json from urlStack Overflow
版权声明:本文标题:javascript - node fetching json from url - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744259543a2597645.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论