Skip to content

Instantly share code, notes, and snippets.

@kasperpeulen
Last active February 20, 2024 02:52
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'
@rodydavis
Copy link

So helpful thank you!

@zouzhenglu
Copy link

if the log content is to long , it still cannot print totally

@lanxuexing
Copy link

JsonEncoder encoder = new JsonEncoder.withIndent('  ');
String prettyprint = encoder.convert(yourJsonString);
debugPrint(prettyprint);

@zouzhenglu you can try it

@miguelnfuertesc
Copy link

It works! Thank you

@johanlantz
Copy link

Spot on, thanks

@sophisticode
Copy link

sophisticode commented Sep 26, 2019

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));
  }

@quynguyen2303
Copy link

Thanks a lot. It is really helpful!

@omishah
Copy link

omishah commented Dec 20, 2019

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));
  }

This solution worked for me. Thank you. <3

    String yourJson = '{"code":"0","text":"hello world"}';

    var object = json.decode(yourJson);
    var prettyString = JsonEncoder.withIndent('  ').convert(object);
    
    print(prettyString);

Output:: https://i.stack.imgur.com/yDebg.png

@Ferdzzzzzzzz
Copy link

Thanks for this, very useful.
I just made this into a small package to keep code clean: pretty_json

@justinemailone
Copy link

Thanks for this, very useful.
I just made this into a small package to keep code clean: pretty_json

Works great, thank you! Very convenient to have on pub.dev!

@oliverbytes
Copy link

I wish you could support Lists too

@Ahtram
Copy link

Ahtram commented Jul 18, 2020

I wish you could support Lists too

+1

@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