Command-line tool to sort JSON files in-place by map key and reusable sorted JSON encoder.

Continuous Integration codecov json_sorter Published by dartside.dev GitHub Stars Count


Just imagine a team where every second pull request comment is about someone forgetting to sort the JSON file correctly... ...and just imagine this team not automating this task in five minutes rather they spend the same amount of time in every pull request nagging each other to sort the damn JSON file.

I hope this does not sound familiar to you, but in case it does, read further!


With this package you can sort JSON files by key in-place using the command-line tool provided by the package.

Ideal if your team is working with JSON files and you want to keep the JSON files organized and you want to do that automatically.

The package also exposes JsonSortedEncoder which is very similar to the JsonEncoder, except it first creates a copy of the passed in object and then recursively sorts every map by key.

You can use the JsonSortedEncoder if you would like to make sure that in your app, all JSON output (be it files or HTTP responses) is sorted. It also supports custom indentation.

If you don't need custom indentation, just call the jsonSortedEncode function with the object you want to encode as JSON, and voila, all your keys are sorted.

You'll need Dart SDK 2.12.0 or higher.


  • json_sorter on the command-line: Install either globally or as a dev dependency
  • JsonSortedEncoder, jsonSortedEncode: Install as a normal dependency

json_sorter command-line tool

Keep in mind that how you invoke the json_sorter depends on how you installed it and whether you are using it from a Flutter or Dart project.

As dev dependency

You can run Dart scripts from your dependencies using the pub run command.

  1. Add json_sorter to your dev_dependencies: pub run add -d json_sorter.
  2. Run the script pub run json_sorter --space-indent 2 filename.json

Global installation

If you install json_sorter globally, you can execute it simply by typing json_sorter example.json.

pub global activate json_sorter
json_sorter --help
json_sorter --version
json_sorter --tab-indent example.json


If you want to create sorted JSONs in your apps, for example in your server's responses, when you print a JSON response in your logs, or when you work with Dart scripts that manipulate JSON files, you can use the JsonSortedEncoder.

It works similarly to the JsonEncoder from dart:convert.

If you don't need indentation, use simply the JsonSortedEncoder constructor. If you need indentation, you can use the JsonSortedEncoder.withIndent(indent) constructor.

// After you added json_sorter to your dependencies
// $ dart pub add json_sorter
import 'package:json_sorter/json_sorter';

// Create encoder
const encoder = JsonSortedEncoder.withIndent('  ');

// Use it on any JSON serializable object, for example lists and maps
const example = {'b': true, 'a': false};

// Use the convert method
const asJson = encoder.convert(example);

// Enjoy


This handy function works exactly like the jsonEncode from dart:convert, the only difference is that it sorts the map keys recursively in the object before encoding.

import 'package:json_sorter/json_sorter.dart';

const inputMap = {
  'xxx': {
    'r': 'r',
    'rr': 'rr',
    'q': 'q',
  'aaa': [1, 1, 1],

void main() {


Don't forget the project's example folder for more, executable examples.