2

So the following is what is returned by the endpoint. Is there a better way to decode this into an array of objects other than turning the data into a string, appending strings to make it a valid JSON, then turning that string back into data and decoding it...

edit: my proposed solution wouldn't even work that well as there are no commas between the objects

edit2: is regex my only answer here F

would be so much easier if it was just encapsulated in { restaurants: [] } :(

    {"address": {"building": "8825", "coord": [-73.8803827, 40.7643124], "street": "Astoria Boulevard", "zipcode": "11369"}, "borough": "Queens", "cuisine": "American", "grades": [{"date": {"$date": 1416009600000}, "grade": "Z", "score": 38}, {"date": {"$date": 1398988800000}, "grade": "A", "score": 10}, {"date": {"$date": 1362182400000}, "grade": "A", "score": 7}, {"date": {"$date": 1328832000000}, "grade": "A", "score": 13}], "name": "Brunos On The Boulevard", "restaurant_id": "40356151"}
    {"address": {"building": "2206", "coord": [-74.1377286, 40.6119572], "street": "Victory Boulevard", "zipcode": "10314"}, "borough": "Staten Island", "cuisine": "Jewish/Kosher", "grades": [{"date": {"$date": 1412553600000}, "grade": "A", "score": 9}, {"date": {"$date": 1400544000000}, "grade": "A", "score": 12}, {"date": {"$date": 1365033600000}, "grade": "A", "score": 12}, {"date": {"$date": 1327363200000}, "grade": "A", "score": 9}], "name": "Kosher Island", "restaurant_id": "40356442"}
    {"address": {"building": "7114", "coord": [-73.9068506, 40.6199034], "street": "Avenue U", "zipcode": "11234"}, "borough": "Brooklyn", "cuisine": "Delicatessen", "grades": [{"date": {"$date": 1401321600000}, "grade": "A", "score": 10}, {"date": {"$date": 1389657600000}, "grade": "A", "score": 10}, {"date": {"$date": 1375488000000}, "grade": "A", "score": 8}, {"date": {"$date": 1342569600000}, "grade": "A", "score": 10}, {"date": {"$date": 1331251200000}, "grade": "A", "score": 13}, {"date": {"$date": 1318550400000}, "grade": "A", "score": 9}], "name": "Wilken'S Fine Food", "restaurant_id": "40356483"}
    {"address": {"building": "6409", "coord": [-74.00528899999999, 40.628886], "street": "11 Avenue", "zipcode": "11219"}, "borough": "Brooklyn", "cuisine": "American", "grades": [{"date": {"$date": 1405641600000}, "grade": "A", "score": 12}, {"date": {"$date": 1375142400000}, "grade": "A", "score": 12}, {"date": {"$date": 1360713600000}
csstudent903
  • 127
  • 14

1 Answers1

1

This is the JSON Lines format. Just split on \n and map over JSONDecoder. Assuming you have this as one long String:

let result = try json.split(separator: "\n")
    .map { Data($0.utf8) }
    .map { try JSONDecoder().decode(Record.self, from: $0) }
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 1
    omg. thank you so much. genius. jesus. I was about to do regex – csstudent903 Mar 05 '21 at 01:01
  • JSON Lines format. that's a new one for me. I've always worked in completely formed Jsons – csstudent903 Mar 05 '21 at 01:02
  • Not everyone calls it that. Lots of people dream up this format on their own :D But there is a very small site devoted to giving it a name. https://jsonlines.org It's a very convenient format when you don't know how many records you're going to generate. – Rob Napier Mar 05 '21 at 01:03
  • works like a charm. thank you so much. still can't get over I was about to use regex – csstudent903 Mar 05 '21 at 01:04
  • 1
    regex is never the answer :D https://stackoverflow.com/a/1732454/97337 – Rob Napier Mar 05 '21 at 01:04