Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Last active April 11, 2022 20:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coolaj86/aa6e6ca71d4b85300b53a290fe8eb97a to your computer and use it in GitHub Desktop.
Save coolaj86/aa6e6ca71d4b85300b53a290fe8eb97a to your computer and use it in GitHub Desktop.

Can you convert a Value Object (i.e. JSON) to YAML?

My Expectation

My expectation is that a mid-senior level developer should be able to accomplish this task in under 4 hours:

  • write a basic solution in 30-60 minutes
  • debug / test / fine-tune for another 60-90 minutes
  • document, package, and publish within the remaining time

I could see it reasonable even if a developer takes 2 days to solve this for the first time - possibly because they needed to take a break but didn't, such as feeling pressure and getting brain freeze, taking an initial wrong approach, over-thinking the problem, and then just keeping beating the dead horse, etc - especially if they've never had to work independently before.

However, the general rule is 4 hours. Otherwise, I don't think they're a good hire for jobs that require any sort of data or API manipulation.

Problem Description

Given a Value Object that looks like this:
(in JavaScript)

var data = {
  foo: [
    {
      bar: [
        {
          baz: ["apples", "bananas"],
        },
      ],
      quux: [
        {
          do: ["red thing", "blue thing"],
        },
      ],
    },
  ],
  grault: {
    name: "Bob",
    age: 37,
    grumpy: false,
    nullish: null,
  },
  answer: 42,
};

Can you produce a function that outputs it as YAML?

foo:
  - bar:
      - baz:
          - "apples"
          - "bananas"
    quux:
      - do:
          - "red thing"
          - "blue thing"
grault:
  name: "Bob"
  age: 37
  grumpy: false
  nullish: null
answer: 42

The solution could have multiple functions, or be recursive, or be iterative. It should not use any libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment