Home > Enterprise >  How to deserialize a JSON where some keys are dupplicated
How to deserialize a JSON where some keys are dupplicated

Time:06-22

The json response looks like this:

{
 "name": "A name",
 "international": {
   "name": {
     "en": "A name",
     "fr": "Un nom"
     }
   }
 }

I tried the following code to deserialize it:

import 'package:json_annotation/json_annotation.dart';
import 'dart:convert';

part 'model.g.dart';

void main() {
  var json = '''{
  "name": "A name",
  "international": {
    "name": {
      "en": "A name",
      "fr": "Un nom"
      }
    }
  }''';
  var model = Model.fromJson(jsonDecode(json));
}

@JsonSerializable()
class Model {
  Model({
    required this.name,
    required this.internationalNames,
  });

  factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);

  final String name;
  @JsonKey(readValue: _readInternational, name: "name")
  final Map<String, String> internationalNames;

  static Object? _readInternational(Map<dynamic, dynamic> json, String key) =>
      json['international'][key];
}

I am getting an error when running the build_runner: More than one field has the JSON key for name "name". even though the internationalNames one is nested inside the international object, so there is no duplicate keys. What could I do to deserialize this?

CodePudding user response:

I get it to work by creating a InternationalModel class:

import 'package:json_annotation/json_annotation.dart';
import 'dart:convert';

part 'temp.g.dart';

void main() {
  var json = '''{
  "name": "A name",
  "international": {
    "name": {
      "en": "A name",
      "fr": "Un nom"
      }
    }
  }''';
  var model = Model.fromJson(jsonDecode(json));
}

@JsonSerializable()
class Model {
  Model({
    required this.name,
    required this.international,
  });

  factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);

  final String name;
  final InternationalModel international;
}

@JsonSerializable()
class InternationalModel {
  InternationalModel({
    required this.names,
  });

  factory InternationalModel.fromJson(Map<String, dynamic> json) =>
      _$InternationalModelFromJson(json);
  @JsonKey(name: 'name')
  final Map<String, String> names;
}

Thanks to Nagual for pointing me towards this solution.

CodePudding user response:

I am able to decode it using below code.

import 'dart:convert';

void main() {
 var json = '''{
  "name": "A name",
  "international": {
    "name": {
      "en": "A name",
      "fr": "Un nom"
      }
    }
  }''';
  Model model = Model.fromJson(jsonDecode(json));
  print(model.international!.name!.en);
}



class Model {
  String? name;
  International? international;

  Model({this.name, this.international});

  Model.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    international = json['international'] != null
        ? new International.fromJson(json['international'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    if (this.international != null) {
      data['international'] = this.international!.toJson();
    }
    return data;
  }
  
 
  
}

class International {
  Name? name;

  International({this.name});

  International.fromJson(Map<String, dynamic> json) {
    name = json['name'] != null ? new Name.fromJson(json['name']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.name != null) {
      data['name'] = this.name!.toJson();
    }
    return data;
  }
}

class Name {
  String? en;
  String? fr;

  Name({this.en, this.fr});

  Name.fromJson(Map<String, dynamic> json) {
    en = json['en'];
    fr = json['fr'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['en'] = this.en;
    data['fr'] = this.fr;
    return data;
  }
}
  • Related