Home > Net >  How to extract value from nested JSON data in Spring Boot
How to extract value from nested JSON data in Spring Boot

Time:01-22

Currently, I am calling an API every 3 minutes to get weather data. This is the response I get as a String:


{
    "data": {
        "timelines": [
            {
                "timestep": "current",
                "endTime": "2023-01-20T13:33:00-05:00",
                "startTime": "2023-01-20T13:33:00-05:00",
                "intervals": [
                    {
                        "startTime": "2023-01-20T13:33:00-05:00",
                        "values": {
                            "dewPoint": 18.63,
                            "humidity": 66,
                            "precipitationIntensity": 0,
                            "precipitationProbability": 0,
                            "pressureSeaLevel": 1019.19,
                            "pressureSurfaceLevel": 1018.65,
                            "temperature": 25.5,
                            "temperatureApparent": 25.5,
                            "uvIndex": 3,
                            "windDirection": 151.69,
                            "windSpeed": 4.88
                        }
                    }
                ]
            }
        ]
    }
}

I would like to know how to extract the value of the attributes in "values" (like "dewPoint", "humidity", etc.).

I've been trying to do this with Beans and such, but the fact that the data is nested and some of it is an array is making it difficult for me to understand. Basically, I want to extract each of the values of the attributes in "values" from a JSON String, concat them in to an array of only the values, and then send that array to be saved in a database.

CodePudding user response:

You will first need to create the classes to model this and then take the string and use an Object mapper to take a string and convert it to an object.

class WeatherData {
    List<Timelines> timelines; // all other object are nested in here.
}

class Timelines {
    String timestep;
    String endTime;
    String startTime;
    List<Intervals> intervals;
}

class Intervals {
    String startTime;
    Values values;
}

class Values {
  double dewPoint;
  int humidity;
  int precipitationIntensity;
  int precipitationProbability;
  double pressureSeaLevel;
  double pressureSurfaceLevel;
  double temperature;
  double temperatureApparent;
  int uvIndex;
  double windDirection; 
  double windSpeed;
}
WeatherData data = new ObjectMapper().readValue(jsonString, WeatherData.class);

There are several ways to approach this once you map the json to an Object. You can loop through the values, so two for loops. Once you get to the intervals you can add the values to the array. List<WeatherData> newWeatherDataList = new ArrayList();

The other way would be to flatten down the object so it's not so nested. I'd suggest looking up how to flatten an object in Java. You will still need one for loop to loop through the intervals array. Then just add it to the new list like above.

From here you can batch-insert the data into a table.

I hope this helps let me know if I can update the answer to provide more context.

  • Related