Turn Data Into JSON Text

In Exercise: Tally the Week’s Ingredients, you worked out Sam’s shopping list from the meal plan and the recipes. Reload the page, though, and that list is gone, along with every recipe Sam added since the last visit. Everything your code builds lives in the page’s memory, which a reload empties.

The browser storage this course uses to keep Sam’s recipes between visits is localStorage, and it holds only strings. You’ll meet it in the next lesson, and that’s all you need about it for now. In this lesson, you’ll turn Sam’s recipes into readable text and back into data, and you’ll predict what changes on the way. Start with one recipe and the function call that writes it down.

Write a Recipe as JSON Text

Before Sam’s soup can be stored, it has to become text, and the usual format for that is JSON, short for JavaScript Object Notation. It looks almost like the object and array literals you already type. JavaScript has a built-in JSON object with two functions: JSON.stringify() turns a value into JSON text, and JSON.parse() turns JSON text back into a value.

Go to about:blank in a new tab and open its Console with Ctrl+Shift+J, or Cmd+Option+J on macOS. Keep that tab for the whole lesson, because each example builds on the ones before. Type in Grandma Ines’s lentil soup, trimmed to the fields this lesson needs, pressing Shift+Enter between lines:

Language: JavaScript
const soup = {
  name: "Grandma Ines's lentil soup",
  servings: 6,
  vegetarian: true,
  ingredients: [
    { name: "lentils", amount: 200, unit: "g" },
    { name: "onion", amount: 1, unit: "whole" },
    { name: "stock", amount: 1, unit: "l" },
  ],
};

Now pass the soup to JSON.stringify(), and ask typeof what came back:

Language: JavaScript
> const text = JSON.stringify(soup);
undefined
> typeof text
'string'
> text
`{"name":"Grandma Ines's lentil soup","servings":6,"vegetarian":true,"ingredients":[{"name":"lentils","amount":200,"unit":"g"},{"name":"onion","amount":1,"unit":"whole"},{"name":"stock","amount":1,"unit":"l"}]}`

The result is one long string, which the Console wraps in backticks because it holds both single and double quotes. Every key sits in double quotes, numbers and true are written as they are, and the ingredients keep their nested shape inside the text.

Don’t treat that text as if it were the recipe object: text.name is undefined, text[0] is the first character, "{", and text.length counts characters, not fields, all without an error. Data goes out through JSON.stringify() and comes back through JSON.parse(), and anything in between is only text.

One long line is hard to read, and Sam may one day want to look at a save, so JSON.stringify() can indent its text. Its third argument is how many spaces to indent each level. The second is a slot for a function that changes values on the way. You don’t need one here, so pass null to leave it empty.

console.log() shows the text without the surrounding quotes, so the linebreaks appear as real lines:

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

Become a Member to join the conversation.