パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.9.0

json_path

RFC 9535 の実装 - JSONPath: JSON 用のクエリ式。`$.store.book[2].price` のようなクエリを使用して、パースされた JSON オブジェクト内の値を読み書きします。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
41.3万
いいね数
126
Pub ポイント
160

パッケージをインストール

flutter pub add json_path

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

json_path は、RFC 9535 を実装した Dart パッケージであり、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で定義されたJSONPath構文に限定
  • JSON以外のデータ型には適していません
  • 信頼できない入力では慎重な検証が必要になる可能性があります

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

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

カテゴリー

依存関係

依存関係一覧 4 件

ソースリポジトリ

このパッケージを使用しているプロジェクト