1

I'm trying to understand why when I'm parsing a JSON response which contains HTML the JSON.parse do not fail but ignore such data which I'm trying to fetch from this endpoint http://registry.npmjs.org/core-js.

Here an example.

fetch('http://registry.npmjs.org/core-js').then(response => 
response.json()).then(data => console.log(data));

The property readme is empty, while if you load the endpoint in the browser, readme is completely populated as:

   "readme": "# core-js\n\nModular standard library for JavaScript. Includes polyfills for [ECMAScript 5, 2015, 2016, 2017](https://github.com/zloirock/core-js#ecmascript): [promises](https://github.com/zloirock/core-js#ecmascript-promise), [symbols](https://github.com/zloirock/core-js#ecmascript-symbol), [collections](https://github.com/zloirock/core-js#ecmascript-collections), iterators, [typed ... n\n**It's a global version (first 2 examples), for more info see [`core-js` documentation](https://github.com/zloirock/core-js/blob/v3/README.md).**\n",

But after parse it with JSON.parse it looks like this

enter image description here

As you see, readme is empty. Why??

Just FYI, I noticed the original issue using Node.js using

body = JSON.parse(body.toString('utf8'));

but then I noticed in the browser I have the same result, however, it just happen in some packages when readme has some sort of characters, encoding or whatever that seems to fails silently. eg: with jquery this works fine.

 fetch('http://registry.npmjs.org/jquery').then(response => 
 response.json()).then(data => console.log(data));

Perhaps someone has an idea what's happening here. I've tried many different ways to remove line breaks and so on, but always with the same result.

Juan Picado
  • 1,823
  • 18
  • 33

2 Answers2

2

I think you're missing that there are multiple readme properties in this object: the one in the root is empty ""

But if you open versions > 3.0.0-alpha.1 (2 or 3) you will see the readme prop is also there and has the value that you provided

"readme": "# core-js\n\nModular standard library for JavaScript. Includes polyfills for [ECMAScript 5, 2015, 2016, 2017](https://github.com/zloirock/core-js#ecmascript): [promises](https://github.com/zloirock/core-js#ecmascript-promise), [symbols](https://github.com/zloirock/core-js#ecmascript-symbol), [collections](https://github.com/zloirock/core-js#ecmascript-collections), iterators, [typed ... n\n**It's a global version (first 2 examples), for more info see [`core-js` documentation](https://github.com/zloirock/core-js/blob/v3/README.md).**\n"
davidluckystar
  • 928
  • 5
  • 15
  • it happens with me all the time :) – davidluckystar Mar 31 '18 at 13:20
  • My mistake was assuming that the response was the same as a few weeks ago and something has changed in the npmjs registry, there is a mirror that still returns readme as before in the root of response. `http://registry.npm.taobao.org/core-js` – Juan Picado Mar 31 '18 at 13:30
1

Agred with david.lucky

There is actually an empty readme

"readme":"",

And 3 filled "readme"

["versions"]["3.0.0-alpha.1"]["readme"]
["versions"]["3.0.0-alpha.2"]["readme"]
["versions"]["3.0.0-alpha.3"]["readme"]
Javier S
  • 379
  • 3
  • 13