0

I am new to ndJSON. I have an ndJSON file with the following data:

{
   "Top-level":{
      "customer_no":"1",
      "created":"2019-03-27 14:24:54",
      "last_visited_day_time":null,
      "login":"roni_cost@example.com"
   },
   "profile":{
      "salutation":"Mr",
      "title":null,
      "company":null,
      "job_title":null,
      "first_name":"Veronica",
      "last_name":"Costello",
      "name_suffix":"NONE",
      "gender":"Female",
      "birthday":"1973-12-15",
      "email":"roni_cost@example.com",
      "next_birthday":"2022-12-15",
      "second_name":null
   },
   "phone":{
      "home_phone":null,
      "business_phone":null,
      "mobile_phone":null,
      "fax_number":null
   },
   "addresses":[
      {
         "address_id":"1",
         "title":"",
         "company":null,
         "salutation":null,
         "first_name":"Veronica",
         "last_name":"Costello",
         "second_name":null,
         "suffix":"NONE",
         "address_1":"6146 Honey Bluff Parkway",
         "address_2":"",
         "suite_no":"",
         "postal_box":"",
         "city":"Calder",
         "postal_code":"49628-7978",
         "country":"US",
         "state":"Michigan",
         "contact_phone":"(555) 229-3326"
      }
   ],
   "orders":{
      "placed_orders_count":2,
      "0":{
         "order_id":"000000001",
         "order_date":"2019-03-27 14:25:03"
      },
      "1":{
         "order_id":"000000002",
         "order_date":"2019-03-27 14:25:03"
      }
   },
   "customs":[
      
   ]
}

I want to read this file into my program and extract data from it.

Following is my code to read the file from my system and trying to write it in CustomerDTO object

Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader("customer.json"));
customerFeedDTO = gson.fromJson(reader, CustomerFeedDTO.class);

But I am getting following exception:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 138 path $.profile

And my CustomerFeedDTO is:

public class CustomerFeedDTO {

    private ArrayList<?> topLevel;
    private ArrayList<?> profile;
    private ArrayList<?> phone;
    private ArrayList<?> addresses;
    private ArrayList<?> orders;
    private ArrayList<?> customs;

//Getters and setters

I am trying to map all the data from the ndJSON file into my customerDTO object

Dragon
  • 25
  • 4
  • 1
    I'm not sure how DTO works, and haven't touched Gson in a while, but it looks like DTO is formatted with ArrayList for profile, and the JSON for profile you supplied is an object {}, which seems pretty similar to the error. – Jon Dec 08 '22 at 18:13

1 Answers1

0

As per the given json, your model class should looks like below.

public class CustomerFeedDTO {

    private Map<String, ?> topLevel;
    private Map<String, ?> profile;
    private Map<String, ?> phone;
    private ArrayList<?> addresses;
    private Map<String, ?> orders;
    private ArrayList<?> customs;
}

topLevel, profile, phone, orders are objects, should be modeled as objects or map, not as list.

Hari Krishna
  • 3,658
  • 1
  • 36
  • 57
  • 1
    That is a good answer although the `topLevel` should be defined same as in the file i.e. "Top-level" to map it but since the, Java Variable naming rules does not allow hyphen between variable names, you must try changing the `Top-level` in the file as well. Otherwise, it will give `null` result. So consider using same spelling of fields as they are in the Json file – Eatsam ul haq Dec 09 '22 at 07:27
  • 1
    Thanks @Eatsamulhaq. I was getting the topLevel as null before but after changing it with right spelling, I was able to get its value. – Dragon Dec 09 '22 at 07:32