{"id":18254975,"url":"https://github.com/vincevargadev/json_sorter_dart","last_synced_at":"2026-01-11T13:36:39.167Z","repository":{"id":56833400,"uuid":"356810159","full_name":"dartsidedev/json_sorter","owner":"dartsidedev","description":"Command-line tool to sort JSON files by key and reusable sorted JSON encoder.","archived":false,"fork":false,"pushed_at":"2021-10-17T18:24:11.000Z","size":26,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2023-08-20T22:28:56.909Z","etag":null,"topics":["dart","flutter","json"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/json_sorter","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dartsidedev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-11T08:24:40.000Z","updated_at":"2021-10-17T18:24:14.000Z","dependencies_parsed_at":"2022-09-08T07:10:33.651Z","dependency_job_id":null,"html_url":"https://github.com/dartsidedev/json_sorter","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dartsidedev%2Fjson_sorter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dartsidedev%2Fjson_sorter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dartsidedev%2Fjson_sorter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dartsidedev%2Fjson_sorter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dartsidedev","download_url":"https://codeload.github.com/dartsidedev/json_sorter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223150704,"owners_count":17095959,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","flutter","json"],"created_at":"2024-11-05T10:14:14.840Z","updated_at":"2025-04-04T17:30:54.850Z","avatar_url":"https://github.com/dartsidedev.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `json_sorter`\n\n\u003e Command-line tool to sort JSON files in-place by map key and reusable sorted JSON encoder. \n\n[![Continuous Integration](https://github.com/dartsidedev/json_sorter/workflows/Continuous%20Integration/badge.svg?branch=main)](https://github.com/dartsidedev/json_sorter/actions) [![codecov](https://codecov.io/gh/dartsidedev/json_sorter/branch/main/graph/badge.svg)](https://codecov.io/gh/dartsidedev/json_sorter) [![json_sorter](https://img.shields.io/pub/v/json_sorter?label=json_sorter\u0026logo=dart)](https://pub.dev/packages/json_sorter 'See json_sorter package info on pub.dev') [![Published by dartside.dev](https://img.shields.io/static/v1?label=Published%20by\u0026message=dartside.dev\u0026logo=dart\u0026logoWidth=30\u0026color=40C4FF\u0026labelColor=1d599b\u0026labelWidth=100)](https://pub.dev/publishers/dartside.dev/packages) [![GitHub Stars Count](https://img.shields.io/github/stars/dartsidedev/json_sorter?logo=github)](https://github.com/dartsidedev/json_sorter 'Star me on GitHub!')\n\n## Important links\n\n* [Read the source code and **star the repo** on GitHub](https://github.com/dartsidedev/json_sorter)\n* [Open an issue on GitHub](https://github.com/dartsidedev/json_sorter/issues)\n* [See package on `pub.dev`](https://pub.dev/packages/json_sorter)\n* [Read the docs on `pub.dev`](https://pub.dev/documentation/json_sorter/latest/)\n* [Flutter Docs on \"JSON and serialization\"](https://flutter.dev/docs/development/data-and-backend/json)\n* [`dart:convert` library docs](https://api.dart.dev/stable/2.12.2/dart-convert/dart-convert-library.html)\n* [Dart Docs `pub run`](https://dart.dev/tools/pub/cmd/pub-run)\n\n## Motivation\n\n*Just imagine a team where every second pull request comment is about someone forgetting to sort the JSON file correctly...*\n*...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.*\n\nI hope this does not sound familiar to you, but in case it does, read further!\n\n## Introduction\n\nWith this package you can sort JSON files by key in-place using the\ncommand-line tool provided by the package.\n\nIdeal if your team is working with JSON files and you want to keep the JSON\nfiles organized and you want to do that automatically.\n\nThe package also exposes [`JsonSortedEncoder`](https://pub.dev/documentation/json_sorter/latest/json_sorter/JsonSortedEncoder-class.html)\nwhich is *very* similar to the `JsonEncoder`, except it first creates a copy\nof the passed in object and then recursively sorts every map by key.\n\nYou can use the `JsonSortedEncoder` if you would like to make sure that in your\napp, all JSON output (be it files or HTTP responses) is sorted.\nIt also supports custom indentation.\n\nIf you don't need custom indentation, just call the [`jsonSortedEncode`](https://pub.dev/documentation/json_sorter/latest/json_sorter/jsonSortedEncode.html)\nfunction with the object you want to encode as JSON, and voila, all your keys are sorted.\n\nYou'll need Dart SDK 2.12.0 or higher.\n\n## Usage\n\n* `json_sorter` on the command-line: Install either globally or as a dev dependency\n* `JsonSortedEncoder`, `jsonSortedEncode`: Install as a normal dependency\n\n### `json_sorter` command-line tool\n\nKeep 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.\n\n#### As dev dependency\n\nYou can run Dart scripts from your dependencies using the `dart run` or `flutter run` command.\n\n1. Add `json_sorter` to your `dev_dependencies`: `dart pub add -d json_sorter` or `flutter pub add -d json_sorter`.\n2. Run the script `dart run json_sorter --space-indent 2 filename.json` or `flutter run json_sorter --space-indent 2 filename.json`\n\n#### Global installation\n\nIf you install `json_sorter` globally, you can execute it simply by typing `json_sorter example.json`.\n\n```\ndart pub global activate json_sorter # or: flutter pub global activate json_sorter\njson_sorter --help\njson_sorter --version\njson_sorter --tab-indent example.json\n```\n\n### `JsonSortedEncoder`\n\nIf you want to create sorted JSONs in your apps, for example in your server's\nresponses, when you print a JSON response in your logs, or when you work with\nDart scripts that manipulate JSON files, you can use the `JsonSortedEncoder`.\n\nIt works similarly to the [`JsonEncoder` from `dart:convert`](https://api.dart.dev/stable/2.12.2/dart-convert/JsonEncoder-class.html).\n\nIf you don't need indentation, use simply the `JsonSortedEncoder` constructor.\nIf you need indentation, you can use the `JsonSortedEncoder.withIndent(indent)` constructor.\n\n```dart\n// After you added json_sorter to your dependencies\n// $ dart pub add json_sorter\nimport 'package:json_sorter/json_sorter';\n\n// Create encoder\nconst encoder = JsonSortedEncoder.withIndent('  ');\n\n// Use it on any JSON serializable object, for example lists and maps\nconst example = {'b': true, 'a': false};\n\n// Use the convert method\nconst asJson = encoder.convert(example);\n\n// Enjoy\nprint(asJson);\n```\n\n### `jsonSortedEncode`\n\nThis handy function works exactly like the [`jsonEncode` from `dart:convert`](https://api.dart.dev/stable/2.9.3/dart-convert/jsonEncode.html),\nthe only difference is that it sorts the map keys recursively in the object before encoding.\n\n```dart\nimport 'package:json_sorter/json_sorter.dart';\n\nconst inputMap = {\n  'xxx': {\n    'r': 'r',\n    'rr': 'rr',\n    'q': 'q',\n  },\n  'aaa': [1, 1, 1],\n};\n\nvoid main() {\n    print(jsonSortedEncode(inputMap));\n}\n```\n\n## `example`\n\nDon't forget the project's [`example`](https://github.com/dartsidedev/json_sorter/tree/master/example) folder for more, executable examples.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvincevargadev%2Fjson_sorter_dart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvincevargadev%2Fjson_sorter_dart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvincevargadev%2Fjson_sorter_dart/lists"}