Home > other >  How to Read ndJSON file in Java
How to Read ndJSON file in Java

Time:12-09

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":"[email protected]"
   },
   "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":"[email protected]",
      "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

CodePudding user response:

The json. simple is a lightweight JSON processing library that can be used to read and write JSON files and it can be used to encode or decode JSON text and fully compliant with JSON specification (RFC4627). In order to read a JSON file, we need to download the json-simple. jar file and set the path to execute it.

CodePudding user response:

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.

  • Related