Skip to content

Instantly share code, notes, and snippets.

@kasperpeulen
Last active June 6, 2024 10:54
Show Gist options
  • Save kasperpeulen/d61029fc0bc6cd104602 to your computer and use it in GitHub Desktop.
Save kasperpeulen/d61029fc0bc6cd104602 to your computer and use it in GitHub Desktop.
How to pretty-print JSON using Dart.
import 'dart:convert';
main() {
Map json = {
'name' : 'Kasper Peulen',
'best_language': 'dart',
'best_chat': 'https://dartlang.slack.com'
};
JsonEncoder encoder = new JsonEncoder.withIndent(' ');
String prettyprint = encoder.convert(json);
print(prettyprint);
}
name: dart.convert_JsonEncoder.withIndent
description: |
How to pretty-print JSON using Dart.
How to display JSON in an easy-to-read (for human readers) format.
tags: 'json pretty-print'
homepage: https://gist.github.com/kasperpeulen/d61029fc0bc6cd104602
environment:
sdk: '>=1.0.0 <2.0.0'
@graphicbeacon
Copy link

Works brilliantly, thank you!

@Luckey-Elijah
Copy link

This is helpful, thank you!

@FirstJavaMaster
Copy link

This is great!

@mostafaac30
Copy link

thank you

@chascruzrm
Copy link

For long logs, this works fine:

  static JsonDecoder decoder = JsonDecoder();
  static JsonEncoder encoder = JsonEncoder.withIndent('  ');

  static void prettyPrintJson(String input) {
    var object = decoder.convert(input);
    var prettyString = encoder.convert(object);
    prettyString.split('\n').forEach((element) => print(element));
  }

Excelent, thank you

@chascruzrm
Copy link

Thank you

@anjarnaufals
Copy link

Thank you

@haashem
Copy link

haashem commented Sep 6, 2023

It was very helpful, Thanks.

@RohanArora13
Copy link

For long logs, this works fine:

  static JsonDecoder decoder = JsonDecoder();
  static JsonEncoder encoder = JsonEncoder.withIndent('  ');

  static void prettyPrintJson(String input) {
    var object = decoder.convert(input);
    var prettyString = encoder.convert(object);
    prettyString.split('\n').forEach((element) => print(element));
  }

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment