如何在 JMESpath 中获取嵌套数据?

时间:2021-02-08 02:21:17

标签: jmespath

我尝试了一些方法,但我的目标只是获得对 last 的访问权限,例如,在对 2021-02-12:5610.0 部分使用通配符时。

{ symbol:symbol,test:putExpDateMap.*.[*].last} 是我最接近成功的地方,但没有雪茄,感谢有关如何编写最有效的查询以仅访问这些属性中的几个而不是所有属性的任何帮助。

{
  "symbol": "TSLA",
  "status": "SUCCESS",
  "underlying": null,
  "strategy": "SINGLE",
  "interval": 0,
  "isDelayed": true,
  "isIndex": false,
  "interestRate": 0.1,
  "underlyingPrice": 853.4449999999999,
  "volatility": 29,
  "daysToExpiration": 0,
  "numberOfContracts": 8,
  "putExpDateMap": {
    "2021-02-12:5": {
      "610.0": [
        {
          "putCall": "PUT",
          "symbol": "TSLA_021221P610",
          "description": "TSLA Feb 12 2021 610 Put (Weekly)",
          "exchangeName": "OPR",
          "bid": 2.22,
          "ask": 2.76,
          "last": 2.36,
          "mark": 2.49,
          "bidSize": 10,
          "askSize": 1,
          "bidAskSize": "10X1",
          "lastSize": 0,
          "highPrice": 2.6,
          "lowPrice": 0.4,
          "openPrice": 0,
          "closePrice": 2.47,
          "totalVolume": 103,
          "tradeDate": null,
          "tradeTimeInLong": 1612558387464,
          "quoteTimeInLong": 1612558799414,
          "netChange": -0.11,
          "volatility": 134.512,
          "delta": -0.035,
          "gamma": 0,
          "theta": -0.836,
          "vega": 0.095,
          "rho": -0.007,
          "openInterest": 709,
          "timeValue": 2.36,
          "theoreticalOptionValue": 2.471,
          "theoreticalVolatility": 29,
          "optionDeliverablesList": null,
          "strikePrice": 610,
          "expirationDate": 1613163600000,
          "daysToExpiration": 5,
          "expirationType": "S",
          "lastTradingDay": 1613178000000,
          "multiplier": 100,
          "settlementType": " ",
          "deliverableNote": "",
          "isIndexOption": null,
          "percentChange": -4.49,
          "markChange": 0.02,
          "markPercentChange": 0.77,
          "mini": false,
          "inTheMoney": false,
          "nonStandard": false
        }
      ]
    },
    "2021-02-19:12": {
      "610.0": [
        {
          "putCall": "PUT",
          "symbol": "TSLA_021921P610",
          "description": "TSLA Feb 19 2021 610 Put",
          "exchangeName": "OPR",
          "bid": 3.1,
          "ask": 3.9,
          "last": 3.4,
          "mark": 3.5,
          "bidSize": 1,
          "askSize": 4,
          "bidAskSize": "1X4",
          "lastSize": 0,
          "highPrice": 3.4,
          "lowPrice": 1.11,
          "openPrice": 0,
          "closePrice": 3.42,
          "totalVolume": 22,
          "tradeDate": null,
          "tradeTimeInLong": 1612558029558,
          "quoteTimeInLong": 1612558799676,
          "netChange": -0.02,
          "volatility": 103.828,
          "delta": -0.044,
          "gamma": 0.001,
          "theta": -0.558,
          "vega": 0.158,
          "rho": -0.016,
          "openInterest": 1677,
          "timeValue": 3.4,
          "theoreticalOptionValue": 3.418,
          "theoreticalVolatility": 29,
          "optionDeliverablesList": null,
          "strikePrice": 610,
          "expirationDate": 1613768400000,
          "daysToExpiration": 12,
          "expirationType": "R",
          "lastTradingDay": 1613782800000,
          "multiplier": 100,
          "settlementType": " ",
          "deliverableNote": "",
          "isIndexOption": null,
          "percentChange": -0.53,
          "markChange": 0.08,
          "markPercentChange": 2.39,
          "mini": false,
          "inTheMoney": false,
          "nonStandard": false
        }
      ]
    },
    "2021-02-26:19": {
      "610.0": [
        {
          "putCall": "PUT",
          "symbol": "TSLA_022621P610",
          "description": "TSLA Feb 26 2021 610 Put (Weekly)",
          "exchangeName": "OPR",
          "bid": 4.15,
          "ask": 5.1,
          "last": 4.2,
          "mark": 4.63,
          "bidSize": 4,
          "askSize": 4,
          "bidAskSize": "4X4",
          "lastSize": 0,
          "highPrice": 4.2,
          "lowPrice": 2.15,
          "openPrice": 0,
          "closePrice": 4.67,
          "totalVolume": 6,
          "tradeDate": null,
          "tradeTimeInLong": 1612558446501,
          "quoteTimeInLong": 1612558795805,
          "netChange": -0.47,
          "volatility": 91.456,
          "delta": -0.053,
          "gamma": 0.001,
          "theta": -0.476,
          "vega": 0.226,
          "rho": -0.03,
          "openInterest": 178,
          "timeValue": 4.2,
          "theoreticalOptionValue": 4.674,
          "theoreticalVolatility": 29,
          "optionDeliverablesList": null,
          "strikePrice": 610,
          "expirationDate": 1614373200000,
          "daysToExpiration": 19,
          "expirationType": "S",
          "lastTradingDay": 1614387600000,
          "multiplier": 100,
          "settlementType": " ",
          "deliverableNote": "",
          "isIndexOption": null,
          "percentChange": -10.15,
          "markChange": -0.05,
          "markPercentChange": -1.05,
          "mini": false,
          "inTheMoney": false,
          "nonStandard": false
        }
      ]
    },
    "2021-03-12:33": {
      "610.0": [
        {
          "putCall": "PUT",
          "symbol": "TSLA_031221P610",
          "description": "TSLA Mar 12 2021 610 Put (Weekly)",
          "exchangeName": "OPR",
          "bid": 6.3,
          "ask": 9.35,
          "last": 5.35,
          "mark": 7.82,
          "bidSize": 5,
          "askSize": 1,
          "bidAskSize": "5X1",
          "lastSize": 0,
          "highPrice": 5.8,
          "lowPrice": 5.35,
          "openPrice": 0,
          "closePrice": 7.79,
          "totalVolume": 3,
          "tradeDate": null,
          "tradeTimeInLong": 1612540259726,
          "quoteTimeInLong": 1612558799462,
          "netChange": -2.44,
          "volatility": 80.707,
          "delta": -0.073,
          "gamma": 0.001,
          "theta": -0.418,
          "vega": 0.37,
          "rho": -0.069,
          "openInterest": 2,
          "timeValue": 5.35,
          "theoreticalOptionValue": 7.786,
          "theoreticalVolatility": 29,
          "optionDeliverablesList": null,
          "strikePrice": 610,
          "expirationDate": 1615582800000,
          "daysToExpiration": 33,
          "expirationType": "S",
          "lastTradingDay": 1615597200000,
          "multiplier": 100,
          "settlementType": " ",
          "deliverableNote": "",
          "isIndexOption": null,
          "percentChange": -31.29,
          "markChange": 0.04,
          "markPercentChange": 0.5,
          "mini": false,
          "inTheMoney": false,
          "nonStandard": false
        }
      ]
    },
    "2021-03-19:40": {
      "610.0": [
        {
          "putCall": "PUT",
          "symbol": "TSLA_031921P610",
          "description": "TSLA Mar 19 2021 610 Put",
          "exchangeName": "OPR",
          "bid": 9.15,
          "ask": 10.05,
          "last": 9.56,
          "mark": 9.6,
          "bidSize": 4,
          "askSize": 4,
          "bidAskSize": "4X4",
          "lastSize": 0,
          "highPrice": 9.56,
          "lowPrice": 6.65,
          "openPrice": 0,
          "closePrice": 9.55,
          "totalVolume": 63,
          "tradeDate": null,
          "tradeTimeInLong": 1612558720834,
          "quoteTimeInLong": 1612558795810,
          "netChange": 0.01,
          "volatility": 77.96,
          "delta": -0.082,
          "gamma": 0.001,
          "theta": -0.404,
          "vega": 0.443,
          "rho": -0.093,
          "openInterest": 4367,
          "timeValue": 9.56,
          "theoreticalOptionValue": 9.552,
          "theoreticalVolatility": 29,
          "optionDeliverablesList": null,
          "strikePrice": 610,
          "expirationDate": 1616184000000,
          "daysToExpiration": 40,
          "expirationType": "R",
          "lastTradingDay": 1616198400000,
          "multiplier": 100,
          "settlementType": " ",
          "deliverableNote": "",
          "isIndexOption": null,
          "percentChange": 0.08,
          "markChange": 0.05,
          "markPercentChange": 0.5,
          "mini": false,
          "inTheMoney": false,
          "nonStandard": false
        }
      ]
    },
    "2021-04-16:68": {
      "610.0": [
        {
          "putCall": "PUT",
          "symbol": "TSLA_041621P610",
          "description": "TSLA Apr 16 2021 610 Put",
          "exchangeName": "OPR",
          "bid": 18.4,
          "ask": 19.3,
          "last": 15.64,
          "mark": 18.85,
          "bidSize": 2,
          "askSize": 5,
          "bidAskSize": "2X5",
          "lastSize": 0,
          "highPrice": 16.84,
          "lowPrice": 15.64,
          "openPrice": 0,
          "closePrice": 18.81,
          "totalVolume": 5,
          "tradeDate": null,
          "tradeTimeInLong": 1612544435621,
          "quoteTimeInLong": 1612558796856,
          "netChange": -3.17,
          "volatility": 74.6,
          "delta": -0.118,
          "gamma": 0.001,
          "theta": -0.391,
          "vega": 0.742,
          "rho": -0.231,
          "openInterest": 294,
          "timeValue": 15.64,
          "theoreticalOptionValue": 18.813,
          "theoreticalVolatility": 29,
          "optionDeliverablesList": null,
          "strikePrice": 610,
          "expirationDate": 1618603200000,
          "daysToExpiration": 68,
          "expirationType": "R",
          "lastTradingDay": 1618617600000,
          "multiplier": 100,
          "settlementType": " ",
          "deliverableNote": "",
          "isIndexOption": null,
          "percentChange": -16.87,
          "markChange": 0.04,
          "markPercentChange": 0.19,
          "mini": false,
          "inTheMoney": false,
          "nonStandard": false
        }
      ]
    },
    "2021-06-18:131": {
      "610.0": [
        {
          "putCall": "PUT",
          "symbol": "TSLA_061821P610",
          "description": "TSLA Jun 18 2021 610 Put",
          "exchangeName": "OPR",
          "bid": 42.65,
          "ask": 44.1,
          "last": 40.25,
          "mark": 43.38,
          "bidSize": 2,
          "askSize": 3,
          "bidAskSize": "2X3",
          "lastSize": 0,
          "highPrice": 41.45,
          "lowPrice": 40.25,
          "openPrice": 0,
          "closePrice": 43.34,
          "totalVolume": 2,
          "tradeDate": null,
          "tradeTimeInLong": 1612551889156,
          "quoteTimeInLong": 1612558799879,
          "netChange": -3.09,
          "volatility": 75.159,
          "delta": -0.168,
          "gamma": 0.001,
          "theta": -0.363,
          "vega": 1.293,
          "rho": -0.681,
          "openInterest": 316,
          "timeValue": 40.25,
          "theoreticalOptionValue": 43.338,
          "theoreticalVolatility": 29,
          "optionDeliverablesList": null,
          "strikePrice": 610,
          "expirationDate": 1624046400000,
          "daysToExpiration": 131,
          "expirationType": "R",
          "lastTradingDay": 1624060800000,
          "multiplier": 100,
          "settlementType": " ",
          "deliverableNote": "",
          "isIndexOption": null,
          "percentChange": -7.13,
          "markChange": 0.04,
          "markPercentChange": 0.08,
          "mini": false,
          "inTheMoney": false,
          "nonStandard": false
        }
      ]
    },
    "2021-07-16:159": {
      "610.0": [
        {
          "putCall": "PUT",
          "symbol": "TSLA_071621P610",
          "description": "TSLA Jul 16 2021 610 Put",
          "exchangeName": "OPR",
          "bid": 51.1,
          "ask": 52.65,
          "last": 49.45,
          "mark": 51.88,
          "bidSize": 1,
          "askSize": 2,
          "bidAskSize": "1X2",
          "lastSize": 0,
          "highPrice": 49.45,
          "lowPrice": 49.45,
          "openPrice": 0,
          "closePrice": 51.81,
          "totalVolume": 4,
          "tradeDate": null,
          "tradeTimeInLong": 1612552046681,
          "quoteTimeInLong": 1612558799421,
          "netChange": -2.36,
          "volatility": 74.204,
          "delta": -0.177,
          "gamma": 0.001,
          "theta": -0.337,
          "vega": 1.474,
          "rho": -0.898,
          "openInterest": 373,
          "timeValue": 49.45,
          "theoreticalOptionValue": 51.812,
          "theoreticalVolatility": 29,
          "optionDeliverablesList": null,
          "strikePrice": 610,
          "expirationDate": 1626465600000,
          "daysToExpiration": 159,
          "expirationType": "R",
          "lastTradingDay": 1626480000000,
          "multiplier": 100,
          "settlementType": " ",
          "deliverableNote": "",
          "isIndexOption": null,
          "percentChange": -4.56,
          "markChange": 0.06,
          "markPercentChange": 0.12,
          "mini": false,
          "inTheMoney": false,
          "nonStandard": false
        }
      ]
    }
  },
  "callExpDateMap": {}
}

1 个答案:

答案 0 :(得分:0)

您的查询确实有两个问题:

  1. putExpDateMap.* 不会为您提供任何级别的未知属性,而只会提供一个级别。由于第一项将通过 putExpDateMap."2021-02-12:5"."610.0" 静态访问,这意味着您必须使用两个通配符来访问嵌套在那里的数组,因此 putExpDateMap.*.*
  2. 如果你需要处理数组,你确实必须使用 [*] 但它不是一个属性,所以应该是 putExpDateMap."2021-02-12:5"."610.0"[*] 而不是 putExpDateMap."2021-02-12:5"."610.0".[*],或者 putExpDateMap.*.*[*]而不是putExpDateMap.*.[*]喜欢你的尝试。

最后但并非最不重要的是,使用更正的查询:

{ symbol:symbol,test:putExpDateMap.*.*[*].last } 

你仍然以混乱的输出结束:

{
  "symbol": "TSLA",
  "test": [
    [
      [
        2.36
      ]
    ],
    [
      [
        3.4
      ]
    ],
    [
      [
        4.2
      ]
    ],
    [
      [
        5.35
      ]
    ],
    [
      [
        9.56
      ]
    ],
    [
      [
        15.64
      ]
    ],
    [
      [
        40.25
      ]
    ],
    [
      [
        49.45
      ]
    ]
  ]
}

因为通配符正在创建一些新级别的数组 — 因为您可以拥有保存数组的 * 属性。

您可能需要阅读 JMESPath flatten operator [] 以解决这个问题。

所以,我们以:

{ symbol:symbol, test:putExpDateMap.*.*[*].last[][] } 

这给出:

{
  "symbol": "TSLA",
  "test": [
    2.36,
    3.4,
    4.2,
    5.35,
    9.56,
    15.64,
    40.25,
    49.45
  ]
}