Search packages, repositories and app categories

Package detailsv0.2.1

rfc_6901

JSON Pointer (RFC 6901). Reads/writes referred values in JSON documents.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
898K
Likes
32
Pub points
160

Install package

flutter pub add rfc_6901

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The rfc_6901 package implements JSON Pointer (RFC 6901) for Dart and Flutter, enabling safe navigation and manipulation of JSON documents. It allows reading and writing values within nested JSON structures using pointer syntax, such as /foo/0/bar. This is particularly useful for dynamic data handling in APIs, configuration files, or state management where precise access to JSON fields is required without parsing the entire structure. The package supports all major platforms including mobile, web, and desktop.

Use cases

  • Accessing nested JSON values dynamically
  • Updating specific fields in a JSON document
  • Parsing API responses with variable structure
  • Implementing partial updates in state management
  • Working with configuration files that use nested keys

Key features

  • Supports RFC 6901 JSON Pointer standard
  • Safe value retrieval and assignment
  • Handles arrays and objects uniformly
  • Cross-platform compatibility
  • Minimal dependency footprint

Suitable for

  • Developers working with dynamic JSON data
  • API clients needing selective field access
  • State management systems requiring patch operations
  • Configuration parsers with deep nesting

Considerations

  • Requires valid JSON Pointer syntax
  • Does not validate target paths at runtime
  • Limited to read/write operations on existing JSON
  • Not suitable for complex JSON transformations

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

RFC 6901 JSON Pointer

JSON Pointer (RFC 6901) implementation in Dart.

Features:

  • Reads, writes, removes values
  • Fully implements the standard, including the - character
  • Can build the entire path, e.g. /foo/bar/-/baz
  • No external dependencies
  • 100% test coverage

Constructing pointers

A JSON Pointer object can be parsed from a string expression. The expression MUST be a valid non-empty JSON Pointer expression with "/" and "~" escaped.

final pointer = JsonPointer('/foo/0/bar'); // a valid non-empty pointer
final alsoPointer = JsonPointer('/foo/-'); // a valid non-empty pointer with a special "-" reference
final emptyPointer = JsonPointer(); // an empty pointer

JsonPointer('oops'); // throws a FormatException: expression doesn't start with "/"
JsonPointer('/foo/~'); // throws a FormatException: unescaped "~"

It can also be built from individual reference tokens. In such case, it will escape the tokens automatically.

final pointer = JsonPointer.build(['foo', '0', 'bar/']); // Makes "/foo/0/bar~1"
final longerPointer = JsonPointerSegment('-', pointer); // Makes "/foo/0/bar~1/-"
final evenLongerPointer = JsonPointerSegment('wow', longerPointer); // Makes "/foo/0/bar~1/-/wow"

Reading and writing values

The read() method returns the referenced value. The write() method returns a copy of the document with the referenced value replaced by the new value.

import 'dart:convert';

import 'package:rfc_6901/rfc_6901.dart';

void main() {
  const json = '{"foo": [{"bar": 42}]}';
  final document = jsonDecode(json);
  print('Original document: $document');

  [
    '/foo', // reads the array
    '/foo/0', // reads the first element of the array
    '/foo/0/bar', // reads 42
  ].forEach((expression) {
    final pointer = JsonPointer(expression);
    print('Pointer "$pointer" reads ${pointer.read(document)}');
  });

  [
    '/foo/0/bar', // {foo: [{bar: banana}]}
    '/foo/-', // {foo: [{bar: 42}, banana]}
    '/a/b/-/c/d', // {foo: [{bar: 42}], a: {b: [{c: {d: banana}}]}}
  ].forEach((expression) {
    final pointer = JsonPointer(expression);
    final d = pointer.write(document, 'banana');
    print('Add a banana at "$pointer": $d');
  });
}

Categories

Source repository

Projects using this package