Querying Json Data From Cms Npi Data
Banging head on this for a couple of days, could use a wake up call ! CMS (Centers for Medicare & Medicaid Services) offers an API for accessing medical practitioner informatio
Solution 1:
headbangin' over. i wuz a json newbie and now i'm a introductee. here's a short code segment in case someone else wants to query and get results from the CMS NPI JSON data. no commercial API needed. the Bloom one I saw referenced did not seem to be active and others required registration and tracking data for you to be able to access public data.
here's the code to access a single field --
import urllib
from bs4 import BeautifulSoup
import json
defmain():
'''
NOTES:
1. pretty switch works set to either true OR on
2. a failed NPI search produces 3-line output like this --
{
"result_count":0, "results":[
]}
'''# valid NPI
url = "https://npiregistry.cms.hhs.gov/api/resultsDemo2/?number=1881761864&pretty=on"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html,"lxml")
# remove HTML from output, producing just a JSON record
practitioner_rec = soup.text
# count lines generated by the query, valid queries are > than 3 lines long
linect = practitioner_rec.count('\n') +1#print "there are ", linect," lines in the input file" # only for testingif linect == 3:
VALID_NPI="FALSE"
VALID_MD="FALSE"else:
'''
query produces a single result, with approx. 60+ lines of output
JSON data a little squirrelly, so we have to
'''
practitioner_rec = practitioner_rec.replace('"result_count":1, ', '')
# print(practitioner_rec) # only for testing
provider_dict = json.loads(practitioner_rec)
provider_info = provider_dict['results'][0]['basic']
print("name:", str(provider_info['name'])) # str-strip out unicode tag
VALID_NPI="TRUE"
VALID_MD="TRUE"print"VALID_NPI is ",VALID_NPI
print"VALID_MD is ",VALID_MD
return [VALID_NPI,VALID_MD]
if __name__ == '__main__':
main()
Solution 2:
Solution 3:
Following Code work for me to in python 3.8:
import requests
import json
test_npi = ['1003849050', '1114119187', None, '1316935836', '1649595216','666','555']
vval = 0
nval = 0
invalidnpi = []
validnpi = []
for n in test_npi:
r = requests.get(f'https://npiregistry.cms.hhs.gov/api//resultsDemo2/?version=2.1&number={n}&pretty=on')
results_text =json.loads( r.text)
try:
print(results_text['result_count']) # This will be always 1 if NPI is validprint(f'NPI number: {results_text["results"][0][ "created_epoch"]}')
print(f'Name : {results_text["results"][0][ "basic"]["name"]}')
print(f'NPI number: {results_text["results"][0][ "basic"]["last_name"]}')
print(f'Phone Number: {results_text["results"][0]["addresses"][1]["telephone_number"]}') # From Primary Practice Address
vval = vval+results_text['result_count']
validnpi.append(n)
# print(f'Last Name: {results_text["results"][0][ "basic"]['last_name']}')except:
nval = nval+1# print(json.loads(results_text)['result_count'])
invalidnpi.append(n)
print(f'{n} is invalid NPI')
print (f'Number of Invalid NPI: {nval}\n Number of Valid NPI: {vval}')
print (f'List of invlid NPI: {invalidnpi}')
print (f'List of invlaid NPI:{validnpi}')
Post a Comment for "Querying Json Data From Cms Npi Data"