RFC 9535 का अमल - JSONPath: JSON के लिए प्रश्न व्यंजक। `$.store.book[2].price` जैसे प्रश्नों का उपयोग करके पार्स किए गए JSON ऑब्जेक्ट में मान पढ़ने और लिखने के लिए।
सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
json_path एक Dart पैकेज है जो JSONPath के लिए RFC 9535 का कार्यान्वयन करता है, जिससे JSON डेटा के शक्तिशाली प्रश्न और संशोधन की अनुमति मिलती है। इसके द्वारा उपयोगकर्ता व्यंजक जैसे `$.store.book[2].price` का उपयोग करके पार्स किए गए JSON ऑब्जेक्ट में मान पढ़ और लिख सकते हैं। यह पैकेज मानक JSONPath सिंटैक्स का समर्थन करता है और Android, iOS, Web, macOS, Windows और Linux जैसे कई प्लेटफॉर्मों पर संगत है। Flutter ऐप्लिकेशन में डायनामिक डेटा निकालने और परिवर्तन के लिए आदर्श है।
उपयोग
नेस्टेड JSON मानों को निकालना
विशिष्ट JSON क्षेत्रों को अपडेट करना
JSON डेटा में ऐरे को फ़िल्टर करना
जटिल JSON संरचनाओं का विश्लेषण
डायनामिक कॉन्फ़िगरेशन हैंडलिंग
API में डेटा परिवर्तन
मुख्य विशेषताएँ
RFC 9535 के पूर्ण अनुपालन
पढ़ने और लिखने की संचालनों का समर्थन करता है
स्पष्ट मार्ग सिंटैक्स जैसे `$.store.book[2].price`
क्रॉस-प्लेटफॉर्म संगतता
हल्का और तेज़
शक्तिशाली त्रुटि संहरण
इनके लिए उपयुक्त
गतिशील JSON प्रश्नों की आवश्यकता वाले Flutter एप्लिकेशन
बैकएंड सेवाएँ JSON प्रतिबिंब संसाधित कर रही हैं
कॉन्फ़िगरेशन मैनेजमेंट सिस्टम
परिवर्तनशील प्रतिक्रिया संरचना वाले API क्लाइंट
लचीले JSON संशोधन की आवश्यकता वाले डेटा पाइपलाइन
विचार योग्य बातें
वैध JSON इनपुट की आवश्यकता होती है
गहन नेस्टेड या बड़े JSON के साथ प्रदर्शन में भिन्नता हो सकती है
RFC 9535 में परिभाषित जीसनपथ सिंटैक्स के सीमित
गैर-JSON डेटा प्रकार के लिए उपयुक्त नहीं
अविश्वसनीय इनपुट में सावधानीपूर्वक प्रमाणीकरण की आवश्यकता हो सकती है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
RFC 9535 - JSONPath: Query Expressions for JSON in Dart
JSONPath defines a string syntax for selecting and extracting JSON (RFC 8259) values from within a given JSON value.
This library is a Dart implementation of the RFC 9535 JsonPath specification. It is also expected to pass the latest version
of the Compliance Test Suite. If you find a missing or incorrectly implemented feature, please open an issue.
For installation instructions and a detailed API documentation, see the pub.dev page.
Usage example:
import'dart:convert';
import'package:json_path/json_path.dart';
voidmain() {
final json = jsonDecode('''
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
''');
final prices = JsonPath(r'$..price');
print('All prices in the store:');
/// The following code will print:////// $['store']['book'][0]['price']: 8.95/// $['store']['book'][1]['price']: 12.99/// $['store']['book'][2]['price']: 8.99/// $['store']['book'][3]['price']: 22.99/// $['store']['bicycle']['price']: 19.95
prices
.read(json)
.map((match) => '${match.path}:\t${match.value}')
.forEach(print);
}
Data manipulation
Each JsonPathMatch produced by the .read() method contains the .pointer property which is a valid JSON Pointer
and can be used to alter the referenced value. If you only need to manipulate JSON data,
check out my JSON Pointer implementation.
User-defined functions
The JSONPath parser may be extended with user-defined functions. The user-defined functions
take precedence over the built-in ones specified by the standard. Currently, only
functions of 1 and 2 arguments are supported.
To create your own function:
Import package:json_path/fun_sdk.dart.
Create a class implementing either Fun1 (1 argument) or Fun2 (2 arguments).
To use it:
Create a new JsonPathParser with your function: final parser = JsonPathParser(functions: [MyFunction()]);
Parse the expression: final jsonPath = parser.parse(r'$[?my_function(@)]');
For more details see the included example.
This package comes with a few non-standard functions which you might find useful.
count(<NodeList>) - returns the number of nodes selected by the argument
index(<SingularNodeList>) - returns the index under which the array element is referenced by the parent array
key(<SingularNodeList>) - returns the key under which the object element is referenced by the parent object
is_array(<Maybe>) - returns true if the value is an array
is_boolean(<Maybe>) - returns true if the value is a boolean
is_number(<Maybe>) - returns true if the value is a number
is_object(<Maybe>) - returns true if the value is an object
is_string(<Maybe>) - returns true if the value is a string
reverse(<Maybe>) - returns the reversed string
siblings(<NodeList>) - returns the siblings for the nodes
xor(<bool>, <bool>) - returns the XOR of two booleans arguments
To use them, import package:json_path/fun_extra.dart and pass the functions to the JsonPath() constructor:
final jsonPath = JsonPathParser(functions: [
constKey(),
constReverse(),
]).parse(r'$[?key(@) == reverse(key(@))]');