0

I have a JSON file of the following format and would like to parse the data to insert into a table. I'm stuck on how to extract data from the file. The data looks like this:

{"business_id": "vcNAWiLM4dR7D2nwwJ7nCA", "full_address": "4840 E Indian School Rd\nSte 101\nPhoenix, AZ 85018", "hours": {"Tuesday": {"close": "17:00", "open": "08:00"}, "Friday": {"close": "17:00", "open": "08:00"}, "Monday": {"close": "17:00", "open": "08:00"}, "Wednesday": {"close": "17:00", "open": "08:00"}, "Thursday": {"close": "17:00", "open": "08:00"}}, "open": true, "categories": ["Doctors", "Health & Medical"], "city": "Phoenix", "review_count": 7, "name": "Eric Goldberg, MD", "neighborhoods": [], "longitude": -111.98375799999999, "state": "AZ", "stars": 3.5, "latitude": 33.499313000000001, "attributes": {"By Appointment Only": true}, "type": "business"}

I have this code until now:

fReader = new FileReader(filePath);
bufferedReader = new BufferedReader(fileReader);
String line;
bufferedReader.readLine();
int index = 0;
while ((line = bufferedReader.readLine()) != null) {
     String businessInfo[] = line.split("\t");
                
     //add a new object based on my OOP design
jay
  • 31
  • 6
  • There are several libraries for this issue. Check this thread: https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Nogard Feb 28 '21 at 17:26
  • What about splitting by '\t' as I have shown above? – jay Feb 28 '21 at 17:30
  • Describe _stuck_. – Savior Feb 28 '21 at 17:37
  • As `'\t'` escape character stands for *tab* and I do not see any *tabs* in your JSON, it won't work. There is no escape character for *space*, but you can use use an empty string `' '` to separate parts of the JSON file. – Nogard Feb 28 '21 at 17:40
  • You can read the file and convert it into string. Then you can use Jackson ObjectMapper to conver string to JsonNode which you can play with. It is very rich in features. ` com.fasterxml.jackson.core jackson-databind ` – Srijan Jul 06 '21 at 19:36

1 Answers1

0

Why don;t you use a JSON parser like Gson or Jackson etc? Pass the payload to it and it will return you back the Object which you will cast to your type.

Amit
  • 633
  • 6
  • 13