Skip to curriculum
Jump to the editor

22.2. Stringify without the password field

Easy
Topics

This question is in

22. Serialization
10–20 min

A user object comes back from the database with a password hash on it, and somewhere a log line or an API response writes it out with JSON.stringify. Deleting the field from the object would change data other code still uses. The safer fix is to leave the field out while serializing, at every depth, without touching the object.

Write stringifyWithout(value, hiddenKeys, space) in stringify-without.js. It returns what JSON.stringify(value, null, space) would return, except that every object property whose key is in hiddenKeys is left out.

Signature

JAVASCRIPT
function stringifyWithout(value, hiddenKeys, space) // returns a string or undefined
  • value: any value JSON.stringify accepts.
  • hiddenKeys (optional): an array of keys to leave out. When it is missing, it is ["password"].
  • space (optional): indentation, with the same meaning as the third argument of JSON.stringify.

Requirements

  1. A property named password is left out, at the top level, in nested objects and in objects inside arrays.
  2. Everything else is written exactly as JSON.stringify writes it, including undefined for undefined.
  3. A given hiddenKeys list replaces the default. An empty list hides nothing.
  4. Keys are matched exactly: Password and passwords are kept.
  5. Only object properties are hidden. Array items are never dropped, even when hiddenKeys holds an index such as "0", and a hidden word used as a value is kept.
  6. The input is not changed.
  7. Objects returned by toJSON are filtered too.
  8. A hidden key that holds an object hides the whole object. Any key can be hidden, including the empty string "": it hides a property named "", never the whole value.
  9. space indents the output; a circular structure still throws a TypeError.

Examples

JAVASCRIPT
stringifyWithout({ user: "ada", password: "hunter2" });
// '{"user":"ada"}'
JAVASCRIPT
stringifyWithout({ users: [{ name: "a", password: "1" }, { name: "b", password: "2" }] });
// '{"users":[{"name":"a"},{"name":"b"}]}'
JAVASCRIPT
stringifyWithout({ password: "p", token: "t", ok: 1 }, ["token"]);
// '{"password":"p","ok":1}'

stringifyWithout({ list: ["a", "b"] }, ["0"]);
// '{"list":["a","b"]}': array items are not object properties

Environment

  • Plain JavaScript in a browser page; the tests call the global stringifyWithout from stringify-without.js.
  • JSON.stringify is allowed and expected. Hiding keys inside a Map or by a pattern is out of scope.

Hints

  1. Which argument of JSON.stringify gets to see every key and value on the way out?
  2. Inside that function, how can you tell an object property from an array item?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…