Skip to content

[Feature Request] Allow specifying json path expression instead of just key name #1075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
soumyamahunt opened this issue Dec 28, 2021 · 1 comment

Comments

@soumyamahunt
Copy link

There are many scenarios where the json map is nested with multiple level of hierarchy, and creating a model only to retrieve one such map is not a good experience. Allowing to specify JsonPath expression in such scenario will be a really welcome change.

For example in the following json:

{
    "coord": {
        "lon": -0.13,
        "lat": 51.51
    },
    "weather": [
        {
            "id": 300,
            "main": "Drizzle",
            "description": "light intensity drizzle",
            "icon": "09d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 280.32,
        "pressure": 1012,
        "humidity": 81,
        "temp_min": 279.15,
        "temp_max": 281.15
    },
    "visibility": 10000,
    "wind": {
        "speed": 4.1,
        "deg": 80
    },
    "clouds": {
        "all": 90
    },
    "dt": 1485789600,
    "sys": {
        "type": 1,
        "id": 5091,
        "message": 0.0103,
        "country": "GB",
        "sunrise": 1485762037,
        "sunset": 1485794875
    },
    "id": 2643743,
    "name": "London",
    "cod": 200
}

if I want weather id, name and temperature my model will look like this:

@JsonSerializable()
class WeatherData {

  String name;

  @JsonKey(path: '$.main.temp')
  double temp;

  @JsonKey(path: '$.weather[0].id')
  int weatherId;

  factory WeatherData.fromJson(Map<String, dynamic> json) =>
      _$WeatherDataFromJson(json);
  Map<String, dynamic> toJson() => _$WeatherDataToJson(this);
}
@kevmoo
Copy link
Collaborator

kevmoo commented Dec 28, 2021

Duplicate of #490

@kevmoo kevmoo marked this as a duplicate of #490 Dec 28, 2021
@kevmoo kevmoo closed this as completed Dec 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants