Not able to do Module 1

The instructions are so confusing I’ve been stuck on this module for 2-3 days now , can anyone tell me how to parse json to pojo
ObjectMapper objectMapper = new ObjectMapper();
Employee emp = objectMapper.readValue(jsonData, Employee.class);
what is jsonData in this code ? am not able to understand tried everything .


Use the above link to understand. Compare the POJO file given in /home/crio-user/workspace/<user_id>-ME_QMONEY/qmoney/bin/main/com/crio/warmup/stock/dto/PortfolioTrade.java
That is the pojo file.
jsonData can be a json output from an api call or json string from an file.

Hi @sidk721,

  • jsonData can be a File, URI, or a String object denoting the JSON data we have to read in.

  • Json file contains a list of data. As java can’t read json files as such, we read each of the data block as an instance of a java class we define, which here is Employee

  • Let’s say the json file has contents,

[ { "name" : "Nabhan", "age" : 23 }, { "name" : "CR7", "age": 35} ] 

Inorder to read this json file to java, we create a Java class(POJO)

public class UserInfo {

 private String name;
 private int age; 

 public void setName(String name) {
   this.name = name;
 }

 public String getName() {
   return name;
 }
 // and other such getters and setters
}
  • So if we have let’s say the json data as a string userDataJson (which by the way is not the case in the milestone)
UserInfo[] users = objectMapper.readValue(userDataJson, UserInfo[].class)
  • Here users[0].getName() will return, … Yes, Nabhan :sweat_smile:

  • Now, why did I use an array, UserInfo[] instead of UserInfo?

Look no further - Error: Com.Fasterxml.Jackson.Databind.Exc.MismatchedInputException

Hope it helps

Just keep it simple, go step by step and visualize the flow of data.

first the data is in a JSON file, how will you read it in java?
then how to convert it to a java object? (Jackson comes handy)
then once you have an object(s), how do you get specific data out of it? (check the pojo files for hints)

hope this meathod of thought will help you understand the problem better.
keep up the good work.