Search code examples
pythonjsonpatentsview

How do I correct the syntax and formatting in this script?


I am trying to specify the fields needed by adding the "f" argument to the GET API call (see here: Patentsview API Python 3.4).

I believe the problem is my formatting and syntax.

I've tried adding curly brackets in several different ways to the "q" and "f" arguments. I return different error messages

import requests

title = "computer" 
author = "Jobs" 
url = "http://www.patentsview.org/api/patents/query" 
data = { 
    "q":{ "_and":[ {"inventor_last_name":author}, {"_text_any":{"patent_title":title}}], 
    "f":["assignee_lastknown_city","assignee_lastknown_state","assignee_lastknown_country"]},
    "o":{"matched_subentities_only":"true"}
} 
resp = requests.post(url, json=data) 
with open("patents.txt", "w") as f:
    f.write(resp.text)

This is what is returned:

{"status":"error","payload":{"error":"'q' parameter: should only have one json object in the top-level dictionary.","code":"RQ3"}}

I would expect a file with results, not an error message.


Solution

  • There were unbalanced '}' in your script. I corrected them. I have not been able to test whether the changes worked or not. Here is the changed code. I have assumed that those were the only errors.

    import requests
    
    title = "computer" 
    author = "Jobs" 
    url = "http://www.patentsview.org/api/patents/query" 
    data = {"q":{ "_and":[ {"inventor_last_name":author}, {"_text_any":{"patent_title":title}}]},"f":["assignee_lastknown_city","assignee_lastknown_state","assignee_lastknown_country"],"o":{"matched_subentities_only":"true"}} 
    resp = requests.post(url, json=data) 
    with open("patents.txt", "w") as f:
        f.write(resp.text)
    

    Please let me know if it worked for you.