从 CMS NPI 数据中查询 JSON 数据 [英] Querying JSON data from CMS NPI data

查看:12
本文介绍了从 CMS NPI 数据中查询 JSON 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题上敲了几天,可以用一个叫醒电话!CMS(医疗保险和医疗补助服务中心)提供了一个 API,用于根据个人的 NPI(国家提供者标识符)访问医生信息.

这里有大量信息,包括每月下载的大文件等,但我不需要任何这些信息.我只需要为我已通过资格预审的单个 NPI 发出查询(低容量)并从检索到的记录中返回一些值.

这是一个随机选择的 NPI 的示例查询——

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 information based on the person's NPI (National Provider Identifier).

There is a wealth of information here including megafile monthly downloads, etc. but I don't need any of that. I simply need to issue a query (low volume) for a single NPI that I have prequalified and return a few values from the record that is retrieved.

Here is a sample query for a randomly-selected NPI -- https://npiregistry.cms.hhs.gov/api/resultsDemo2/?number=1881761864&pretty=on

If you run this in a browser window you see the resulting JSON data encapsulated in some header/footer HTML.

I can dump the entire query result and print it a few different ways but haven't been able to pick out and print specific data elements such as name, address or telephone number. If you run the query in a browser you can see the raw output and the snippet below prints the sanitized version of the results. See below. Ideas?

import urllib
from bs4 import BeautifulSoup
import json

def main():

url = "https://npiregistry.cms.hhs.gov/api/resultsDemo2/?number=1881761864&pretty=on"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html,"lxml")

for script in soup(["script", "style"]):
    script.extract()

practitioner_rec = soup.get_text()

# strip out the html to retain the JSON record
lines = (line.strip() for line in practitioner_rec.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
practitioner_rec = '\n'.join(chunk for chunk in chunks if chunk)

# get a count of lines generated by the query, valid queries are greater than 3 lines long
linect = practitioner_rec.count('\n') +1

if linect == 3:
    VALID_NPI="FALSE"
    VALID_MD="FALSE"
else: # approx. 69 lines of output here
    #   possible issue with JSON formmatting here
    #   In particular, the line   
    #   "result_count":1, "results":[
    #   since result count will always be 1, discard it
    practitioner_rec = practitioner_rec.replace('"result_count":1, ', '')
    print(practitioner_rec)

    practitioner_data = json.loads(practitioner_rec)
    VALID_NPI="TRUE"
    VALID_MD="TRUE"

    '''
    none of these constructs works to print the provider name
    print ['result_count']['results']['basic']['name'],"name"
    print result_count['results']['basic']['name'],"name"
    print practitioner_data['results']['basic']['name'],"name"
    print results['basic']['name'],"name"
    print ['basic']['name'],"name"
    print basic['name'],"name"
    print results[2]['basic']['name'],"name"
    print results['basic']['name'],"name"

    this works, but not useful if I can't pick values out
    print(json.dumps(practitioner_data))      

    print "VALID_NPI is ",VALID_NPI
    print "VALID_MD is  ",VALID_MD
    return [VALID_NPI,VALID_MD]    
    '''


if __name__ == '__main__':
    main()

解决方案

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 valid
        print(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}')

这篇关于从 CMS NPI 数据中查询 JSON 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆