sort_json 1.0.0 copy "sort_json: ^1.0.0" to clipboard
sort_json: ^1.0.0 copied to clipboard

A tool to sort JSON files alphabetically while prioritizing specific keys.

example/sort_json_example.dart

import 'dart:convert';
import 'dart:io';

import 'package:sort_json/utils/json_sorter.dart';
import 'package:sort_json/utils/config_loader.dart';

void main() async {
  // Example: Load some JSON from a file.
  final jsonFilePath = 'sample.json';
  final jsonFile = File(jsonFilePath);
  final jsonContent = await jsonFile.readAsString();
  final decodedJson = jsonDecode(jsonContent);

  // Load prioritized keys from your configuration file.
  final loader = ConfigLoader('config/prioritized_keys.json');
  final prioritizedKeys = await loader.load() as List<dynamic>;

  // Ensure keys are strings.
  final prioritized = prioritizedKeys.whereType<String>().toList();

  // Sort the JSON using the JsonSorter.
  final sortedJson = JsonSorter.sortJson(decodedJson, prioritized: prioritized);

  // Print or use the sorted JSON.
  print(JsonEncoder.withIndent('  ').convert(sortedJson));
}
4
likes
0
points
481
downloads

Publisher

unverified uploader

Weekly Downloads

A tool to sort JSON files alphabetically while prioritizing specific keys.

Repository (GitHub)
View/report issues

Topics

#json #cli #sorting #dart

License

unknown (license)

Dependencies

yaml

More

Packages that depend on sort_json