extract urls from json file without data name using python

1.6k views Asked by At

i have json file that containd the metadata of 900 articles and i want to extract the Urls from it. my file start like this

[
{
    "title": "The histologic phenotypes of …",
    "authors": [
        {
            "name": "JE Armes"
        },
    ],
    "publisher": "Wiley Online Library",
    "article_url": "https://onlinelibrary.wiley.com/doi/abs/10.1002/(SICI)1097-0142(19981201)83:11%3C2335::AID-CNCR13%3E3.0.CO;2-N",
    "cites": 261,
    "use": true
},

{
    "title": "Comparative epidemiology of pemphigus in ...",
    "authors": [
        {
            "name": "S Bastuji-Garin"
        },
        {
            "name": "R Souissi"
        }
        ],
        "year": 1995,
        "publisher": "search.ebscohost.com",
    "article_url": "http://search.ebscohost.com/login.aspx?direct=true&profile=ehost&scope=site&authtype=crawler&jrnl=0022202X&AN=12612836&h=B9CC58JNdE8SYy4M4RyVS%2FrPdlkoZF%2FM5hifWcv%2FwFvGxUCbEaBxwQghRKlK2vLtwY2WrNNl%2B3z%2BiQawA%2BocoA%3D%3D&crl=c",
    "use": true
    },
 .........

I want to inspect the file with objectpath to create json.tree for the extraxtion of the url. this is the code i want to execute

  1.    import json
  2.    import objectpath
  3.    with open("Data_sample.json") as datafile: data = json.load(datafile)
  4.    jsonnn_tree = objectpath.Tree(data['name of data'])
  5.    result_tuple = tuple(jsonnn_tree.execute('$..article_url'))

But in the step 4 for the creation of the tree, I have to insert the name of the data whitch i think that i haven't in my file. How can i replace this line?

3

There are 3 answers

0
Bryan On

You can get all the article urls using a list comprehension.

import json

with open("Data_sample.json") as fh:
    articles = json.load(fh)

article_urls = [article['article_url'] for article in articles]
0
andreihondrari On

You can instantiate the tree like this:

tobj = op.Tree(your_data)
results = tobj.execute("$.article_url")

And in the end:

results = [x for x in results]

will yield:

["url1", "url2", ...]
0
bspeagle On

Did you try removing the reference and just using:

jsonnn_tree = objectpath.Tree(data)