Implementation Of JSONPath Expressions Like "$.store.book[2].price" for Flutter

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);
}

Features and Limitations 

This library follows the JsonPath internet draft specification v11. Since the spec itself is an evolving document, this implementation may lag behind, and some features may not be implemented in-full. Please refer to the tests (there are hundreds of them, including the CTS) to see what is supported.

Currently supported standard functions:

  • length()
  • count()
  • match()
  • search()
  • value()

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 by 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 argument).

To use it:

  1. Create a new JsonPathParser with your function: final parser = JsonPathParser(functions: [MyFunction()]);
  2. Use it to parse you expression: final jsonPath = parser.parse(r'$[?my_function(@)]');

For more details see the included example.

This package comes with some non-standard functions which you might find useful. To use them, import package:json_path/fun_extra.dart.

References 

Use this package as a library

Depend on it

Run this command:

With Dart:

 $ dart pub add json_path

With Flutter:

 $ flutter pub add json_path

This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):

dependencies:
  json_path: ^0.6.4

Alternatively, your editor might support dart pub get or flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:json_path/json_path.dart';

example/example.dart

import 'dart:convert';

import 'package:json_path/json_path.dart';

void main() {
  final document = 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
    }
  }
}  
  ''');

  print('All prices in the store, by JSONPath:');
  JsonPath(r'$..price')
      .read(document)
      .map((match) => '${match.path}:\t${match.value}')
      .forEach(print);

  print('\nSame, by JSON Pointer:');
  JsonPath(r'$..price')
      .read(document)
      .map((match) => '${match.pointer}:\t${match.value}')
      .forEach(print);

  print('\nSame, but just the values:');
  JsonPath(r'$..price').readValues(document).forEach(print);

  print('\nBooks under 10:');
  JsonPath(r'$.store.book[?@.price < 10].title')
      .readValues(document)
      .forEach(print);

  print('\nBooks with ISBN:');
  JsonPath(r'$.store.book[?@.isbn].title').readValues(document).forEach(print);

  print('\nBooks under 10 with ISBN:');
  JsonPath(r'$.store.book[?@.price < 10 && @.isbn].title')
      .readValues(document)
      .forEach(print);

  print('\nBooks with "the" in the title:');
  JsonPath(r'$.store.book[?search(@.title, "the")].title')
      .readValues(document)
      .forEach(print);

  print('\nBooks with the same category as the last one:');
  JsonPath(r'$.store.book[?@.category == $.store.book[-1].category].title')
      .readValues(document)
      .forEach(print);
}

Download details:

Author: karapetov.com

Source: https://github.com/f3ath/jessie

#flutter #android #ios 
 

Implementation Of JSONPath Expressions Like "$.store.book[2].price" for Flutter
1.10 GEEK