求符合条件的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:
- Syntax Error: It’s missing closing parentheses
)and bracket], which will cause any JSONPath parser to fail immediately. - Scope Mismatch: Using
$..vehicleLeasesearches globally for allvehicleLeasenodes across the entire JSON, not just the ones nested under the same parent object asvehicleCode. This could lead to false matches if unrelated objects elsewhere have the samevehicleLeasevalue.
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 underStateOffers(your data structuresStateOffersas an array of arrays, so this ensures we check every nested object).[?(@.vehiclePart[*].segments[*].vehicleLease == "AS PER LAW OF ENFORCMENT")]: Filters objects where anysegmentsentry inside anyvehicleParthas avehicleLeasematching your target string. The@symbol references the current object being evaluated, ensuring we only checkvehicleLeasevalues under that specific parent object (not the entire JSON)..vehicleCode: Extracts thevehicleCodeproperty 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
相关产品推荐
相关产品推荐

