首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python,jsonpath:如何正确解析jsonpath?

Python,jsonpath:如何正确解析jsonpath?
EN

Stack Overflow用户
提问于 2018-02-06 02:47:48
回答 2查看 12.8K关注 0票数 2

我有一个实现问题...

代码语言:javascript
复制
#!/usr/bin/python

#This is the API for BTC price request. 
# Average all the amounts, and push that to the program

import json
import urllib.request
from jsonpath_rw import parse as parse_jsonpath

class BtcAPI:

    def __init__(self, url, api_id, json_key):
        self.url = url
        self.api_id = api_id
        self.json_key = json_key

    def btc_api_call(self):

        hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
        req = urllib.request.Request(self.url, headers=hdr)
        readdata = urllib.request.urlopen(req)
        json_data = readdata.read()

        json_dict = json.loads(json_data)
        results = parse_jsonpath(self.json_key).find(json_dict)
        print(results)


class Price:


    def __init__(self, api_id, url, json_key):

        self.api_id = api_id
        self.url = url
        self.json_key = json_key

    def pass_for_request(self):

        get_price = BtcAPI(self.url, self.api_id, self.json_key)
        get_price.btc_api_call()


def Coindesk():
    coindesk = Price("coindesk","https://api.coindesk.com/v1/bpi/currentprice.json","time.updated")
coindesk.pass_for_request()

为"json_key“传递的值是"bpi.USD.rate_float"...在此url中。它被传递给一个名为"Price“的类,这个类创建了一些变量,这些变量传递给包含上述代码的类。

代码语言:javascript
复制
coindesk = Price("coindesk","api.coindesk.com/v1/bpi/currentprice.json", "bpi.USD.rate_float")

这就是我的目标json。正在尝试访问rate_float密钥:

代码语言:javascript
复制
{
  "time": {
    "updated": "Feb 5, 2018 18:34:00 UTC",
    "updatedISO": "2018-02-05T18:34:00+00:00",
    "updateduk": "Feb 5, 2018 at 18:34 GMT"
  },
  "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
  "chartName": "Bitcoin",
  "bpi": {
    "USD": {
      "code": "USD",
      "symbol": "$",
      "rate": "7,004.9588",
      "description": "United States Dollar",
      "rate_float": 7004.9588
  }
}

当我运行该程序时,它会向我发送整个json文件的输出,而不是我试图通过jsonpath发送给我的特定键,我使用的是jsonpath_rw,使用的是"bpi.USD.rate_float“。

如何使用jsonpath有效地定位rate_float密钥?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-06 05:11:42

问题是,您可能认为results包含来自Python dict的普通字符串值。

这实际上不是jsonpath_rw返回的内容。如果你看一下https://github.com/kennknowles/python-jsonpath-rw中的例子,你会发现它是这样做的,例如

代码语言:javascript
复制
[match.value for match in jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})]

正如它提到的那样:

JsonPath.find的结果提供了详细的上下文和路径数据,因此很容易遍历到父对象,打印数据片段的完整路径,并生成自动ids。

find实际上返回一个包含上下文信息的对象列表。如果您想要的只是字符串值,并且您只希望为JSONPath表达式获得一个返回值,则需要执行以下操作:

代码语言:javascript
复制
results = parse_jsonpath(self.json_key).find(json_dict)
print(results[0].value)
票数 2
EN

Stack Overflow用户

发布于 2018-08-28 12:07:19

下面是一个有效的Python 3方法:

代码语言:javascript
复制
def json_by_jsonpath():
    v_json     = "{\"_id\":{\"oid\":\"55561331552632234577\"},\"profilePatId\":\"123456\",\"patientPatId\":\"123456\",\"comboOrder\":false,\"globalOrderCode\":{\"$numberInt\":\"1\"},\"orderRefNum\":\"621980991862\",\"functionality\":{\"$numberInt\":\"38\"},\"rxType\":{\"$numberInt\":\"1\"},\"orderStoreNum\":\"59382\",\"orderChannel\":\"RR_EM\",\"orderChannelDtls\":\"QULL\",\"scriptDetailRetail\":[{\"rxNum\":{\"$numberInt\":\"274879\"},\"rxStoreNum\":{\"$numberInt\":\"59382\"},\"autoRefillEnabled\":false,\"autoRefillEligible\":false,\"insuranceAvailable\":false,\"sddDrugEligible\":false,\"drugQty\":{\"$numberInt\":\"0\"},\"deliveryInd\":\"1\",\"deliveryComments\":\"Ring bell and drop of in box. This comment is for SDD notes\",\"additionalComments\":\"Please call my insurance and this comment is on notes section\",\"appCode\":{\"$numberInt\":\"291\"}},{\"rxNum\":{\"$numberInt\":\"447902\"},\"rxStoreNum\":{\"$numberInt\":\"59382\"},\"autoRefillEnabled\":false,\"autoRefillEligible\":false,\"insuranceAvailable\":false,\"sddDrugEligible\":false,\"drugQty\":{\"$numberInt\":\"0\"},\"deliveryInd\":\"1\",\"deliveryComments\":\"Ring bell and drop of in box. This comment is for SDD notes\",\"additionalComments\":\"Please call my insurance and this comment is on notes section\",\"appCode\":{\"$numberInt\":\"100\"}}],\"_class\":\"com.rx.repositories.xOrderRetail\"}"


v_jsonpath =  '$._id.oid'  #'$.patientPatId'

        myjson = json.loads(v_json)
    #print(myjson)
    #result_list = [match.value for match in v_jsonpath.find(v_json)]
    #print(result_list)
    json_tag_val = parse(v_jsonpath).find(myjson)
    print(json_tag_val)

    return json_tag_val[0].value
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48629461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档