In this video, you changed the booking prompt to ask for each record as a JSON object, parsed every reply in read-notes.js, and met the run where parsing broke. Then you guarded the parse so that one bad reply holds back its own note instead of stopping the batch.
Key Points
- A record as lines of text isn’t data yet. To use one field, you’d search the lines for it, as you did for the preferred date. JSON is data written as text, and JavaScript reads it with one call. The Modules and JSON course covers the format if you’d like a refresher.
- Ask for JSON in the prompt. The prompt’s field lines became the keys of a JSON object, one camelCase name per field, as your code will write the property names. The two examples’ records became JSON objects too, so the examples show the format the rules ask for.
- Parse after the
cutOffcheck. The loop keeps its check for a cut-off reply:ask()setscutOfftotruewhen the output limit stopped a reply before it finished. After the check,JSON.parse()turns the text into a JavaScript value, which is an object for these replies. The loop prints its kind, pet name, and urgency. The first run parsed all twelve notes. - One reply can stop the batch. On a later run,
JSON.parse()threw aSyntaxErrorat the fourth note, Pepper and Salt’s. Notes five to twelve never ran, and the total never printed, but you still paid for four calls, a little over a cent. - The reply is text, whatever the prompt asks. That reply used 391 output tokens, and most of its text was nonsense words, with the JSON object only at the very end. It’s a rare, garbled reply, and nothing in your request rules it out.
JSON.parse()needs the whole string to be JSON, so the first stray word throws. Stray text can also be a sentence or a code fence around the object, which throws too, though this course’s runs only showed nonsense words. - Guard the parse. Declare
recordfirst, so the line after thetryblock can still use it, and putJSON.parse()inside thetry. If it throws, thecatchblock prints the note’s number and the error’s name, andcontinuemoves on to the next note. - Hold back anything that isn’t an object. Parsing only proves the text was JSON.
nullis valid JSON too:JSON.parse("null")returnsnull, and readingrecord.kindfrom it would throw outside thetryand stop the batch. So the loop holds back a value that isn’t an object. - Keep the
cutOffcheck first. A cut-off reply is unfinished JSON, so it would fail to parse anyway, but checking first tells you that the reply hit the output limit. - The guarded run held back one note. In the guarded run below, Pepper and Salt’s reply began with the same kind of noise, and it used up the 400 output tokens partway through the preferred time, at
"preferredTime": "10:. The check held it back, and the other eleven notes and the total came through. A refusal, which any call can get, comes back without the record, so the guard holds that back too. A later lesson names refusals. - Any reply can fail. In 152 runs of the set with one provider’s capable model, every other note parsed. Pepper and Salt’s failed twelve times: eight cut off, and four not JSON, like the reply that stopped your first script. Now the
catchholds a reply like that back. Your runs will differ.
A prompt can ask for JSON, but it can’t make the model send it. Check cutOff first, then parse inside try...catch, and hold back any note that doesn’t give you an object. The Errors and Debugging course has more on try...catch if you’d like a refresher.