How can I read json data which has its tag/identifier in quotes?

102 Views Asked by At

I'm using NodeJS to fetch the NPM registry with the code below.

import fetch from "node-fetch";

let url = "https://registry.npmjs.com/[package-name]";

let settings = { method: "Get" };

fetch(url, settings)
    .then(res => res.json())
    .then((json) => {
    console.log(json.versions)
    });

So, the response that I get in my result is:

{
  _id: '123456789',
  name: 'package-name',
  'dist-tags': { latest: '2.0.0' },
  versions: {
    '2.0.0': '...'
}
...
}

I am trying to read the 'versions' section, but the issue I have is that the version number 2.0.0 is encased in quotes and it might change but I want to find that value. How can I read it?

Thanks in advance, and let me know any other info I missed.

1

There are 1 best solutions below

0
On

You'll have to check if the versions has the key you want.

if(Object.keys(response.versions).includes('2.0.0')){
  // response.versions['2.0.0'] exists
}

Or you could iterate through Object.keys(response.versions) and handle each version entry as you wish.