1
votes

I am trying to decode pubmed json output as follows.

  1. https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=29753496&retmode=json

  2. https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=15674886&retmode=json

How can I extract the doi from this output?

        "articleids": [
            {
                "idtype": "pubmed",
                "idtypen": 1,
                "value": "15674886"
            },
            {
                "idtype": "doi",
                "idtypen": 3,
                "value": "10.1002/14651858.CD001801.pub2"
            },
            {
                "idtype": "rid",
                "idtypen": 8,
                "value": "15674886"
            },
            {
                "idtype": "eid",
                "idtypen": 8,
                "value": "15674886"
            }
        ],

I was able to extract other details like title, author name etc. But this one seems little tricky.

Sorry if its a silly question.

3
(articleids.filter(a => a.idtype === 'doi').pop() || {}).value assuming there is max one idtype === 'doi' - Ele

3 Answers

1
votes

So, lets assume this is your whole JSON string.

var json = '{"articleids": [
    { "idtype": "pubmed", "idtypen": 1, "value": "15674886" },
    { "idtype": "doi", "idtypen": 3, "value": "10.1002/14651858.CD001801.pub2" },
    { "idtype": "rid", "idtypen": 8, "value": "15674886" },
    { "idtype": "eid", "idtypen": 8, "value": "15674886" }
]}';

Now we want to parse this and get it into an object.

var arr = JSON.parse(json);

To get a specific object based on the value of an item, we will want to use .filter(). We'll use .pop() to return the first element of the returned array, which in this case should be the only object returned.

var doi = arr.articleids.filter(function(v)
{
    return v.idtype == "doi";
}).pop();

Variable doi is now going to hold the filtered object.

idtype: "doi"
idtypen: 3
value: "10.1002/14651858.CD001801.pub2"
0
votes

Try this :

var jsonObj = {
	"articleids": [{
			"idtype": "pubmed",
			"idtypen": 1,
			"value": "15674886"
		},
		{
			"idtype": "doi",
			"idtypen": 3,
			"value": "10.1002/14651858.CD001801.pub2"
		},
		{
			"idtype": "rid",
			"idtypen": 8,
			"value": "15674886"
		},
		{
			"idtype": "eid",
			"idtypen": 8,
			"value": "15674886"
		}
	]
};

var res = jsonObj.articleids.filter(obj => obj.idtype == 'doi');

console.log(res[0]); // {idtype: "doi", idtypen: 3, value: "10.1002/14651858.CD001801.pub2"}
-1
votes

In your example, the value "doi" can be referenced as:

articleids[1].idtype

Here's an example you can use yourself.

note: I'm wrapping your data structure in an object called obj

var obj = {
    "articleids": [{
        "idtype": "pubmed",
        "idtypen": 1,
        "value": "15674886"
    }, {
        "idtype": "doi",
        "idtypen": 3,
        "value": "10.1002/14651858.CD001801.pub2"
    }, {
        "idtype": "rid",
        "idtypen": 8,
        "value": "15674886"
    }, {
        "idtype": "eid",
        "idtypen": 8,
        "value": "15674886"
    }]
}

//access doi
obj.articleids[1].idtype // -> 'doi'

//loop throigh idTypes and log them to the console
obj.articleids.forEach(function(article) {
    console.log(article.idtype)
}) 

JSON allows you to access keys in [brackets] but you can also use the '.' property accessor when you have a string as the object key. Given:

var me = {"name":"AK"}

AK can be referenced as

me["name"]

or

me.name