Skip to content

Instantly share code, notes, and snippets.

@thash
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save thash/ceb5a937abb9b50186a6 to your computer and use it in GitHub Desktop.

Select an option

Save thash/ceb5a937abb9b50186a6 to your computer and use it in GitHub Desktop.
$ brew install jq
$ curl "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ebola"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE eSearchResult PUBLIC "-//NLM//DTD esearch 20060628//EN" "http://eutils.ncbi.nlm.nih.gov/eutils/dtd/20060628/esearch.dtd">
<eSearchResult><Count>2819</Count><RetMax>20</RetMax><RetStart>0</RetStart><IdList>
<Id>25587816</Id>
<Id>25585832</Id>
...
<Id>25580233</Id>
</IdList><TranslationSet><Translation> <From>ebola</From> <To>"hemorrhagic fever, ebola"[MeSH Terms] OR ("hemorrhagic"[All Fields] AND "fever"[All Fields] AND "ebola"[All Fields]) OR "ebola hemorrhagic fever"[All Fields] OR "ebola"[All Fields] OR "ebolavirus"[MeSH Terms] OR "ebolavirus"[All Fields]</To> </Translation></TranslationSet><TranslationStack> <TermSet> <Term>"hemorrhagic fever, ebola"[MeSH Terms]</Term> <Field>MeSH Terms</Field> <Count>1033</Count> <Explode>Y</Explode> </TermSet> <TermSet> <Term>"hemorrhagic"[All Fields]</Term> <Field>All Fields</Field> <Count>60599</Count> <Explode>N</Explode> </TermSet> <TermSet> <Term>"fever"[All Fields]</Term> <Field>All Fields</Field> <Count>164827</Count> <Explode>N</Explode> </TermSet> <OP>AND</OP> <TermSet> <Term>"ebola"[All Fields]</Term> <Field>All Fields</Field> <Count>2698</Count> <Explode>N</Explode> </TermSet> <OP>AND</OP> <OP>GROUP</OP> <OP>OR</OP> <TermSet> <Term>"ebola hemorrhagic fever"[All Fields]</Term> <Field>All Fields</Field> <Count>1076</Count> <Explode>N</Explode> </TermSet> <OP>OR</OP> <TermSet> <Term>"ebola"[All Fields]</Term> <Field>All Fields</Field> <Count>2698</Count> <Explode>N</Explode> </TermSet> <OP>OR</OP> <TermSet> <Term>"ebolavirus"[MeSH Terms]</Term> <Field>MeSH Terms</Field> <Count>1169</Count> <Explode>Y</Explode> </TermSet> <OP>OR</OP> <TermSet> <Term>"ebolavirus"[All Fields]</Term> <Field>All Fields</Field> <Count>1259</Count> <Explode>N</Explode> </TermSet> <OP>OR</OP> <OP>GROUP</OP> </TranslationStack><QueryTranslation>"hemorrhagic fever, ebola"[MeSH Terms] OR ("hemorrhagic"[All Fields] AND "fever"[All Fields] AND "ebola"[All Fields]) OR "ebola hemorrhagic fever"[All Fields] OR "ebola"[All Fields] OR "ebolavirus"[MeSH Terms] OR "ebolavirus"[All Fields]</QueryTranslation></eSearchResult>
require 'open-uri'
require 'json'
require 'hashie'
response = open('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ebola&retmode=json')
data = response.read
json = JSON.parse(data)
mash = Hashie::Mash.new(json)
mash.esearchresult.idlist # => => ["25587816", "25585832", "25585771", ...
$ cat users.json | jq '.users[].name'
"Alice"
"Bob"
$ curl -s "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ebola&retmode=json" \
| jq '.esearchresult.idlist[]'
"25587816"
"25585832"
"25585771"
"25585757"
"25585723"
"25585496"
"25585384"
"25584879"
"25584356"
$ cat users.json | jq '.users[] | select(.age < 25)'
{
"name": "Alice",
"age": 24,
"lang": "Ruby"
}
require 'open-uri'
require 'json'
response = open('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ebola&retmode=json')
data = response.read
json = JSON.parse(data)
puts json.class # => Hash
puts json.keys # => header esearchresult
puts json['esearchresult']['count'] # => 2819
puts json['esearchresult']['idlist'] # => 25587816 25585832 25585771 ...
$ curl -s "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ebola&retmode=json" | head -n 20
{
"header": {
"type": "esearch",
"version": "0.3"
},
"esearchresult": {
"count": "2819",
"retmax": "20",
"retstart": "0",
"idlist": [
"25587816",
"25585832",
"25585771",
"25585757",
"25585723",
"25585496",
"25585384",
"25584879",
"25584356",
"25584153",
require 'open-uri'
response = open('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ebola&retmode=json')
# response.class #=> StringIO
# response.read.class #=> String
puts response.read
#=> {
# "header": {
# "type": "esearch",
# "version": "0.3"
# },
# "esearchresult": {
# "count": "2819",
# "retmax": "20",
# "retstart": "0",
# "idlist": [
# "25587816",
{"users": [{"name": "Alice", "age": 24, "lang": "Ruby"}, {"name": "Bob", "age": 28, "lang": "Python"}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment