请求正确JSON Path:通过name/description定位提取指定JSON的value值
Let's get that JSON Path sorted out for you! Your original path has a few syntax and structure issues—let's break it down and fix it step by step.
First, let's make sure we're working with valid JSON (your snippet was missing the outer object wrapper and had a trailing comma, so here's the cleaned-up version):
{ "Vehicle": { "code": "BCA", "name": "COLL", "description": "Collision", "limitTerms": [], "deductibleTerms": [ { "code": "qsw", "name": "", "value": "500", "valueCode": "", "valueDescription": "" } ], "otherTerms": [] } }
What Was Wrong With Your Original Path?
Your path $.vehicle[description='Collision' and .deductibleTerms[*].value] had three key issues:
- Case sensitivity: JSON Path is case-sensitive—your root node is
Vehicle, notvehicle. - Incorrect conditional syntax: You need to filter the
Vehiclenode first using itsdescriptionproperty, then traverse todeductibleTerms. - Misplaced traversal: The
deductibleTerms[*].valuepart shouldn't be inside the filter condition for theVehiclenode.
Correct JSON Paths
Here are the working paths to extract the "500" value, using either description or name to locate the right Vehicle node:
1. Using description="Collision" to Locate the Node
$.Vehicle[?(@.description == 'Collision')].deductibleTerms[*].value
$.Vehicle: Targets the root-levelVehicleobject.[?(@.description == 'Collision')]: Filters to only keep theVehiclewhere thedescriptionmatches"Collision"..deductibleTerms[*]: Iterates over all items in thedeductibleTermsarray..value: Extracts thevaluefield from each array item.
2. Using name="COLL" to Locate the Node
If you prefer to use the name field instead, just swap the filter condition:
$.Vehicle[?(@.name == 'COLL')].deductibleTerms[*].value
3. Simplified Version (For Fixed Structures)
If you know there's only one Vehicle node and it will always have the deductibleTerms array with the value you need, you can skip the filter entirely for a more concise path:
$.Vehicle.deductibleTerms[0].value
This directly grabs the value from the first (and only) item in the deductibleTerms array.
These paths work in most popular JSON Path implementations (like Jayway JsonPath, jsonpath-ng, etc.).
内容的提问来源于stack exchange,提问作者Umair

