---
id: 6a15cadf5f240d05a264955d
title: "Challenge 319: Frontmatter Parser"
challengeType: 29
dashedName: challenge-319
---

# --description--

Given a string representing a frontmatter block, parse it and return an object (JavaScript) or dictionary (Python) with the keys and values.

Frontmatter is wrapped in `---` delimiters and contains `key: value` pairs within them, one per line. For example:

```md
---
title: My Post
draft: false
views: 100
---
```

Should return:

```js
{
  title: "My Post",
  draft: false,
  views: 100
}
```

- Numbers, Booleans, and Strings should all be returned as their respective type.
- The given string will have new lines separated with the newline character (`"\n"`). The above example would be given as: `"---\ntitle: My Post\ndraft: false\nviews: 100\n---"`.

# --hints--

`parse_frontmatter("---\ntitle: My Post\ndraft: false\nviews: 100\n---")` should return `{ title: "My Post", draft: False, views: 100 }`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_frontmatter("---\\ntitle: My Post\\ndraft: false\\nviews: 100\\n---"), {"title": "My Post", "draft": False, "views": 100})`)
}})
```

`parse_frontmatter("---\nid: 6a174db57256a112f932195c\ntitle: My Book\nlocale: en\nwordCount: 10000\npublished: false\n---")` should return `{ id: "6a174db57256a112f932195c", title: "My Book", locale: "en", wordCount: 10000, published: False }`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_frontmatter("---\\nid: 6a174db57256a112f932195c\\ntitle: My Book\\nlocale: en\\nwordCount: 10000\\npublished: false\\n---"), {"id": "6a174db57256a112f932195c", "title": "My Book", "locale": "en", "wordCount": 10000, "published": False})`)
}})
```

`parse_frontmatter("---\nversion: 1.0.0\nurl: https://example.com\nprivate: true\n---")` should return `{ version: "1.0.0", url: "https://example.com", private: True }`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_frontmatter("---\\nversion: 1.0.0\\nurl: https://example.com\\nprivate: true\\n---"), {"version": "1.0.0", "url": "https://example.com", "private": True})`)
}})
```

`parse_frontmatter("---\nrating: 4.5\nprice: 9.99\n---")` should return `{ rating: 4.5, price: 9.99 }`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_frontmatter("---\\nrating: 4.5\\nprice: 9.99\\n---"), {"rating": 4.5, "price": 9.99})`)
}})
```

# --seed--

## --seed-contents--

```py
def parse_frontmatter(s):

    return s
```

# --solutions--

```py
def parse_frontmatter(s):
    lines = [line for line in s.split("\n") if line != "---" and line != ""]
    result = {}
    for line in lines:
        key, *rest = line.split(":")
        value = ":".join(rest).strip()
        if value == "true":
            result[key.strip()] = True
        elif value == "false":
            result[key.strip()] = False
        else:
            try:
                n = float(value)
                result[key.strip()] = int(n) if n == int(n) else n
            except ValueError:
                result[key.strip()] = value
    return result
```
