JSON Path Extraction
Overview
Two methods are provided for extracting values from JSON using path expressions:
ExtractFromJsonToArray
- Returns extracted values as a string arrayExtractFromJson
- Returns extracted values joined with commas as a single string
Path Syntax
Basic Structure
- Paths can start with
$
(optional) representing the root - Properties can be accessed using dot notation or bracket notation
- Property names with special characters must use bracket notation with quotes
Supported Notations
-
Dot Notation
$.property $.nested.property
-
Bracket Notation
$['property'] $["property"]
-
Array Access
$.array[0] // Access specific index $.array[*] // Access all elements
-
Mixed Notation
$.nested['property'] $['nested'].property
Special Cases
-
Properties Containing Dots
$['property.with.dots'] $.nested['complex.name']
-
Properties Containing Brackets
$['property[with]brackets'] $.nested['array[0]name']
-
Complex Nested Paths
$.nested['complex[name].with.dot'] $.nested.array[0]['special.name[0]']
Usage Examples
1. Simple Property Access
var json = @"{
""name"": ""John"",
""age"": 30
}";
// Both return ["John"]
ExtractFromJsonToArray(json, "$.name")
ExtractFromJsonToArray(json, "$['name']")
// Returns "John"
ExtractFromJson(json, "$.name")
2. Nested Properties
var json = @"{
""user"": {
""details"": {
""name"": ""John""
}
}
}";
// Returns ["John"]
ExtractFromJsonToArray(json, "$.user.details.name")
// Returns "John"
ExtractFromJson(json, "$.user.details.name")
3. Array Access
var json = @"{
""users"": [
{ ""name"": ""John"" },
{ ""name"": ""Jane"" }
]
}";
// Returns ["John", "Jane"]
ExtractFromJsonToArray(json, "$.users[*].name")
// Returns "John,Jane"
ExtractFromJson(json, "$.users[*].name")
4. Special Characters in Property Names
var json = @"{
""user.name"": ""John"",
""special[key]"": ""value"",
""nested"": {
""complex[name].with.dot"": ""data""
}
}";
// All valid path expressions:
ExtractFromJson(json, "$['user.name']")
ExtractFromJson(json, "$['special[key]']")
ExtractFromJson(json, "$.nested['complex[name].with.dot']")
Return Value Handling
-
Null or Empty Inputs
- If either JSON or path is null, return null, if empty then return empty array / string
-
Non-existent Paths
- Returns empty array/string
-
Value Types
- String values: Returned as-is
- Numbers: Converted to string representation
- Boolean: Converted to lowercase string (“true”/”false”)
- Null: Skipped in the output
- Objects: Converted to compact JSON string
- Arrays: Converted to compact JSON string when directly accessed
Error Handling
-
Invalid JSON
- Returns empty array/string
-
Invalid Path Syntax
- Returns empty array/string
-
Array Index Out of Bounds
- Returns empty array/string
Best Practices
- Use bracket notation with quotes for properties containing special characters
- Use dot notation for simple property names
- Use
ExtractFromJsonToArray
when you need to process individual values - Use
ExtractFromJson
when you need a comma-separated string of values
Limitations
- Path must be valid according to the supported syntax
- Array wildcards (
[*]
) cannot be used with object properties - No support for advanced JSON path features like filters or recursive descent
- All numeric values are converted to their string representation