Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Ask for JSON in the Prompt

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 cutOff check. The loop keeps its check for a cut-off reply: ask() sets cutOff to true when 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 a SyntaxError at 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 record first, so the line after the try block can still use it, and put JSON.parse() inside the try. If it throws, the catch block prints the note’s number and the error’s name, and continue moves on to the next note.
  • Hold back anything that isn’t an object. Parsing only proves the text was JSON. null is valid JSON too: JSON.parse("null") returns null, and reading record.kind from it would throw outside the try and stop the batch. So the loop holds back a value that isn’t an object.
  • Keep the cutOff check 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 catch holds 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.

The Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your booking prompt turns each owner’s note into a record of seven lines of text. To use one field in code, you search the lines for it, as you did for the preferred date.

00:11 JSON is data written as text, and JavaScript reads it with one call. So ask for JSON. In the prompt, the field lines become the keys of a JSON object, one camelCase name per field, as your code will write them.

00:27 The two rules that name fields now say keys. The two examples’ records become JSON objects to match.

00:37 Now read-notes.js. Its loop printed each reply’s text, unless the reply was cut off. That check stays, now as an if that prints the note’s number and moves on.

00:49 ask() sets cutOff when the output limit stopped a reply. After the check, JSON.parse() turns the text into a JavaScript value, and for these replies that’s an object.

01:01 The loop prints the kind, the pet’s name, and the urgency. Run it, and every note comes back as an object, then the total cost.

01:13 A single good run doesn’t tell you much, so run the set again. This time, the script stops at the fourth note, Pepper and Salt’s. JSON.parse() threw a syntax error because the text isn’t valid JSON.

01:27 The uncaught error stopped the whole batch. Notes five to twelve never ran, and the total never printed. You still paid for four calls, a little over a cent.

01:41 So look at what the model sent. This reply used 391 output tokens, and most of its text is nonsense words. The JSON object comes only at the very end. It’s a rare, garbled reply, and nothing in your request rules it out.

01:58 JSON.parse() needs the whole string to be JSON, so the first stray word throws. More often, the stray text is a sentence or a code fence around the object, and that throws too.

02:10 The prompt asks for JSON, but the reply is still text.

02:16 So guard the parse. Declare record first, so the code after the try can use it, then parse inside the try. If it throws, the catch prints the note’s number and the error’s name, and continue moves on.

02:31 Parsing only proves the text was JSON. null is valid JSON too, and reading kind from it would throw outside the try and stop the batch.

02:40 So hold back anything that isn’t an object.

02:44 Keep the cutOff check first. A cut-off reply would fail to parse anyway, but checking first tells you it was the limit, not the text.

02:55 Run the set again. This time, Pepper and Salt’s reply began with the same noise, and it used up the four hundred output tokens partway through the preferred time.

03:05 The cutOff check held it back, and the other eleven notes and the total came through.

03:11 In a hundred and fifty-two runs 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.

03:24 Now the catch holds a reply like that back. Your runs will differ, and any reply can fail.

03:32 A prompt can ask for JSON, but it can’t make the model send it. So check cutOff first, then parse inside try...catch, and hold back any note that doesn’t give you an object.

03:43 One bad reply no longer stops the batch.

03:48 But a held-back note is still a booking nobody has read. Next, in the lesson Request JSON That Fits a Schema, you’ll ask for the record’s shape itself, so the reply can’t start with stray text.

04:01 A reply the limit cuts off will still need the cutOff check.

Become a Member to join the conversation.