How to Convert JSON into Dart Class null safety

Json to dart converter null safety

In this article, we learn that how to convert JSON object to Dart class using JSON to Dart Convert tool. It is very difficult to writing dart code manually. This tool is really very simple to use. JSON to Dart null safety tool converts JSON response into Dart class on single button click. Just copy your JSON and paste into input JSON box and click on Generate Dart button. Dart Null Safety already enabled in Dart Class. Dart null safety will indicate that a variable may have the value null.

Sample JSON Object

{
  "RIFUSDS": [
    {
      "timestamp": "2021-08-13T16:00:00.000Z",
      "open": "0.2257370",
      "close": "0.2257370",
      "min": "0.2257370",
      "max": "0.2257370",
      "volume": "59",
      "volumeQuote": "25.9611130"
    },
    {
      "timestamp": "2021-02-13T12:00:00.000Z",
      "open": "0.3015120",
      "close": "0.3216128",
      "min": "0.3015120",
      "max": "0.3216768",
      "volume": "4079",
      "volumeQuote": "1298.0319504"
    }
  ],
  "BERRYUSDS": [
    {
      "timestamp": "2021-02-13T04:00:00.000Z",
      "open": "0.00061800",
      "close": "0.00061780",
      "min": "0.00061000",
      "max": "0.00071783",
      "volume": "10460",
      "volumeQuote": "6.89477840"
    },
    {
      "timestamp": "2021-02-12T20:00:00.000Z",
      "open": "0.00060489",
      "close": "0.00061800",
      "min": "0.00048829",
      "max": "0.00061800",
      "volume": "466690",
      "volumeQuote": "228.12405820"
    }
  ]
}

Generated Dart File

class AutoGenerate {
  AutoGenerate({
    required this.RIFUSDS,
    required this.BERRYUSDS,
  });
  late final List<RIFUSDS> RIFUSDS;
  late final List<BERRYUSDS> BERRYUSDS;
  
  AutoGenerate.fromJson(Map<String, dynamic> json){
    RIFUSDS = List.from(json['RIFUSDS']).map((e)=>RIFUSDS.fromJson(e)).toList();
    BERRYUSDS = List.from(json['BERRYUSDS']).map((e)=>BERRYUSDS.fromJson(e)).toList();
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['RIFUSDS'] = RIFUSDS.map((e)=>e.toJson()).toList();
    _data['BERRYUSDS'] = BERRYUSDS.map((e)=>e.toJson()).toList();
    return _data;
  }
}

class RIFUSDS {
  RIFUSDS({
    required this.timestamp,
    required this.open,
    required this.close,
    required this.min,
    required this.max,
    required this.volume,
    required this.volumeQuote,
  });
  late final String timestamp;
  late final String open;
  late final String close;
  late final String min;
  late final String max;
  late final String volume;
  late final String volumeQuote;
  
  RIFUSDS.fromJson(Map<String, dynamic> json){
    timestamp = json['timestamp'];
    open = json['open'];
    close = json['close'];
    min = json['min'];
    max = json['max'];
    volume = json['volume'];
    volumeQuote = json['volumeQuote'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['timestamp'] = timestamp;
    _data['open'] = open;
    _data['close'] = close;
    _data['min'] = min;
    _data['max'] = max;
    _data['volume'] = volume;
    _data['volumeQuote'] = volumeQuote;
    return _data;
  }
}

class BERRYUSDS {
  BERRYUSDS({
    required this.timestamp,
    required this.open,
    required this.close,
    required this.min,
    required this.max,
    required this.volume,
    required this.volumeQuote,
  });
  late final String timestamp;
  late final String open;
  late final String close;
  late final String min;
  late final String max;
  late final String volume;
  late final String volumeQuote;
  
  BERRYUSDS.fromJson(Map<String, dynamic> json){
    timestamp = json['timestamp'];
    open = json['open'];
    close = json['close'];
    min = json['min'];
    max = json['max'];
    volume = json['volume'];
    volumeQuote = json['volumeQuote'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['timestamp'] = timestamp;
    _data['open'] = open;
    _data['close'] = close;
    _data['min'] = min;
    _data['max'] = max;
    _data['volume'] = volume;
    _data['volumeQuote'] = volumeQuote;
    return _data;
  }
}

One Comment on “How to Convert JSON into Dart Class null safety”

  1. JSON (JavaScript Object Notation) is a text format for storing data in the KEY, VALUE pair. JSON Beautifier tool helps to beauty and format the JSON in the readable JSON text.

Leave a Reply

Your email address will not be published. Required fields are marked *