You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求符合条件的JSON查询语句:获取指定vehicleLease对应的vehicleCode

Fixing JSONPath Query to Retrieve vehicleCode for Matching vehicleLease Value

Let's walk through fixing your JSONPath query to correctly fetch the vehicleCode values where the associated vehicleLease equals "AS PER LAW OF ENFORCMENT".

First, Identify the Issues in Your Original Query

Your current expression has two critical problems:

  1. Syntax Error: It’s missing closing parentheses ) and bracket ], which will cause any JSONPath parser to fail immediately.
  2. Scope Mismatch: Using $..vehicleLease searches globally for all vehicleLease nodes across the entire JSON, not just the ones nested under the same parent object as vehicleCode. This could lead to false matches if unrelated objects elsewhere have the same vehicleLease value.

Correct JSONPath Expression

Here’s the revised query that fixes syntax issues and targets the right hierarchy:

$.StateOffers[*][*][?(@.vehiclePart[*].segments[*].vehicleLease == "AS PER LAW OF ENFORCMENT")].vehicleCode

Breakdown of the Query

Let’s break down each part to clarify how it works:

  • $.StateOffers[*][*]: Traverses all elements in the nested arrays under StateOffers (your data structures StateOffers as an array of arrays, so this ensures we check every nested object).
  • [?(@.vehiclePart[*].segments[*].vehicleLease == "AS PER LAW OF ENFORCMENT")]: Filters objects where any segments entry inside any vehiclePart has a vehicleLease matching your target string. The @ symbol references the current object being evaluated, ensuring we only check vehicleLease values under that specific parent object (not the entire JSON).
  • .vehicleCode: Extracts the vehicleCode property from the filtered, matching objects.

Alternative (For Known Single-Element Arrays)

If you’re certain that vehiclePart and segments will always be single-element arrays (like in your sample data), you can use this more explicit version for tighter targeting:

$.StateOffers[*][*][?(@.vehiclePart[0].segments[0].vehicleLease == "AS PER LAW OF ENFORCMENT")].vehicleCode

Test Result

When run against your sample JSON, both queries will return:

[242214214]

内容的提问来源于stack exchange,提问作者Ashutosh Dalai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:35:27