Movatterモバイル変換


[0]ホーム

URL:


json_path 0.9.0copy "json_path: ^0.9.0" to clipboard
json_path: ^0.9.0 copied to clipboard

Metadata

Implementation of RFC 9535 - JSONPath: Query Expressions for JSON. Reads and writes values in parsed JSON objects using queries like `$.store.book[2].price`.

More...

RFC 9535 -JSONPath: Query Expressions for JSON in Dart#

Pub PackageGitHub IssuesGitHub ForksGitHub StarsGitHub License

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 9535JsonPath specification. It is also expected to pass the latest versionof theCompliance Test Suite. If you find a missing or incorrectly implemented feature, please open an issue.

For installation instructions and a detailed API documentation, see thepub.dev page.

Usage example:#

import 'dart:convert';import 'package:json_path/json_path.dart';void main() {  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#

EachJsonPathMatch produced by the.read() method contains the.pointer property which is a validJSON Pointerand can be used to alter the referenced value. If you only need to manipulate JSON data,check out myJSON Pointer implementation.

User-defined functions#

The JSONPath parser may be extended with user-defined functions. The user-defined functionstake precedence over the built-in ones specified by the standard. Currently, onlyfunctions of 1 and 2 arguments are supported.

To create your own function:

  1. Importpackage:json_path/fun_sdk.dart.
  2. Create a class implementing eitherFun1 (1 argument) orFun2 (2 arguments).

To use it:

  1. Create a new JsonPathParser with your function:final parser = JsonPathParser(functions: [MyFunction()]);
  2. 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, importpackage:json_path/fun_extra.dart and pass the functions to theJsonPath() constructor:

final jsonPath = JsonPathParser(functions: [  const Key(),  const Reverse(),]).parse(r'$[?key(@) == reverse(key(@))]');

References#

124
likes
160
points
203k
downloads

Publisher

verified publisherkarapetov.com

Weekly Downloads

Metadata

Implementation of RFC 9535 - JSONPath: Query Expressions for JSON. Reads and writes values in parsed JSON objects using queries like `$.store.book[2].price`.

Repository (GitHub)
View/report issues
Contributing

Documentation

API reference

License

MIT (license)

Dependencies

iregexp,maybe_just_nothing,petitparser,rfc_6901

More

Packages that depend on json_path

Metadata

124
likes
160
points
203k
downloads

Publisher

verified publisherkarapetov.com

Weekly Downloads

Metadata

Implementation of RFC 9535 - JSONPath: Query Expressions for JSON. Reads and writes values in parsed JSON objects using queries like `$.store.book[2].price`.

Repository (GitHub)
View/report issues
Contributing

Documentation

API reference

License

MIT (license)

Dependencies

iregexp,maybe_just_nothing,petitparser,rfc_6901

More

Packages that depend on json_path

Back


[8]ページ先頭

©2009-2026 Movatter.jp