DEV Community

Sandeep Yadav
Sandeep Yadav

Posted on • Edited on

6 2

JSON to dart converter - dart null safety

JSON to dart converter
DART class
Dart is a programming language designed for client development, such as for the web and mobile apps. Dart is used to build server and desktop applications. Dart is an object-oriented, class-based, garbage-collected language with C-style syntax.

JSON object
JSON is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays.

How to Convert JSON into Dart Class null safety?

It is very difficult to writing dart code manually. So we build a tool JSON to dart converter which convert JSON into dart class with null safety feature. JSON to Dart null safety tool convert json to dart in single button click.

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"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

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;
  }
}
Enter fullscreen mode Exit fullscreen mode

`
Image description

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more