5

Trying to convert my json to objects in Dart/Flutter using the json_serializable. I cannot seem to find a way to access a nested ID (data is coming from MongoDB thus the $ in the json).

Here is the json:

{
    "_id": {
        "$oid": "5c00b227"  <-- this is what I am trying to access 
    },
    "base": 1,
    "tax": 1,
    "minimum": 5,
    "type": "blah"
}

Result:

class Thing {
  final int id;
  final String base;
  final String tax;
  final String type;
  final int minimum;
}

2 Answers 2

4

It is not possible with json_serializable package itself. You have to create separate objects for getting this nested data. Look the discussion here https://github.com/google/json_serializable.dart/issues/490

But, there is possible way to get nested fields with added converter (solution was found here https://github.com/google/json_serializable.dart/blob/master/example/lib/nested_values_example.dart)

import 'package:json_annotation/json_annotation.dart';

part 'nested_values_example.g.dart';

/// An example work-around for
/// https://github.com/google/json_serializable.dart/issues/490
@JsonSerializable()
class NestedValueExample {
  NestedValueExample(this.nestedValues);

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

  @_NestedListConverter()
  @JsonKey(name: 'root_items')
  final List<String> nestedValues;

  Map<String, dynamic> toJson() => _$NestedValueExampleToJson(this);
}

class _NestedListConverter
    extends JsonConverter<List<String>, Map<String, dynamic>> {
  const _NestedListConverter();

  @override
  List<String> fromJson(Map<String, dynamic> json) => [
        for (var e in json['items'] as List)
          (e as Map<String, dynamic>)['name'] as String
      ];

  @override
  Map<String, dynamic> toJson(List<String> object) => {
        'items': [
          for (var item in object) {'name': item}
        ]
      };
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this,

class Thing {
   int id;
   String base;
   String tax;
   String type;
   int minimum;

   Thing({
    this.id,
    this.base,
    this.tax,
    this.type,
    this.minimum,
  });

   factory Thing.fromJson(Map<String, dynamic> json) {
     return Thing(
       id: json['_id']["oid"],
       base: json['base'].toString(),
       tax: json['tax'].toString(),
       type: json['type'].toString(),
       minimum: json['minimum'],
     );
   }
}

2 Comments

Is there a way to do it in the jsons/thing.json file using JsonKey(name: 'id') final String "id" (but using it on nested json)? If I use your code whenever I run json_model if the json ever changes then it will overwrite the factory method. If I remove the "" from the "id" in the original json then it works. I guess json_model does NOT like the "".
This does not answer the above question. The question is how to achieve that using ` json_serializable`.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.