패키지, 저장소 및 앱 카테고리 검색

GitHub 저장소f3ath/jessie

제시

RFC 9535 - JSONPath: Dart에서의 JSON 쿼리 표현식 구현

제시 프로젝트 이미지
스타
39
포크
6
최근 푸시(UTC)
2025. 12. 11.
프로젝트 상태
활성

원본 README

아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.

RFC 9535 - JSONPath: Query Expressions for JSON in Dart

Pub Package GitHub Issues GitHub Forks GitHub Stars GitHub 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 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';

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

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:

  1. Import package:json_path/fun_sdk.dart.
  2. Create a class implementing either Fun1 (1 argument) or Fun2 (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, import package:json_path/fun_extra.dart and pass the functions to the JsonPath() constructor:

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

References

Alexey GitHub avatar
GitHub 작성자

Alexey ↗

If I had ever learnt, I should have been a great proficient.

Salesforce Silicon Valley, CA
언어Dart

분류

이 저장소가 배포한 패키지

사용 중인 의존성

의존성 목록 8 개

프로젝트에 맞는 저장소를 선택하려면?

사용 목적에 따라 검색하고 개발 활동을 비교한 뒤 원본 저장소에서 요구 사항을 확인하세요.

  1. 이름, 작성자 또는 설명으로 검색하고 기술 주제로 범위를 좁힙니다.
  2. 스타, 포크, 최근 푸시를 함께 비교하세요. 스타는 관심의 지표이며 품질을 보장하지 않습니다.
  3. 사용하기 전에 GitHub에서 README, 라이선스, Issues를 확인하세요.

출처 및 관리

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗