0

I want to store content and metadata for a website in a "markdown with JSON header" sort of format. Extracting the JSON header with regex seems like a Very Bad Idea. Are there any JSON parsers for PHP that will simply parse a JSON object until the closing brace of the outermost object, and then just stop? Preferably that will also give me the string offset of the end of the JSON substring.

I could require some sort of delimiter (like a blank line) between the JSON and markdown strings, but it seems like it should be unnecessary since JSON already seems to delimit itself.

I don't know if I'm using the right language to describe this problem; perhaps that's why I'm having difficulty finding anything

Here's an example just for fun:

{
"title": "example post",
"tags":["a","b","c"]
}
This is an *example*. The metadata for this post is stored inline with the data itself! How cool is that??!

- here's
- a
- list

If it helps, this only needs to work with PHP7

user371366
  • 200
  • 2
  • 8
  • A horrible solution would be `function awfulSolution($string) { for($i=0; $i – user371366 Jul 07 '16 at 21:48
  • Looking for [this](http://stackoverflow.com/questions/2583472/regex-to-validate-json) maybe ? – cachique Jul 07 '16 at 21:53
  • I saw that, and it looks nice, but it seems like unnecessary overhead to do a complex regex match when the JSON parser is going to scan and tokenize the string again right after. Especially since I assume JSON parsers are going to be highly optimized for parsing JSON, compared to a regex engine which would be optimized for a much wider set of uses. – user371366 Jul 07 '16 at 21:59
  • If you want to store json/md in the same document, could you just put the markdown inside the json instead? I know that doesn't really answer your question; just wondering. – Don't Panic Jul 07 '16 at 22:49
  • @Don'tPanic That'd be pretty painful to work with for any sizable document. Especially if you started dealing with Markdown features that use indentation, like lists. –  Jul 08 '16 at 03:28
  • @duskwuff Good point. I agree. – Don't Panic Jul 08 '16 at 14:46
  • I'll probably just implement some simple key:value type syntax myself and write a simple parser that does what I need, but I think my question is probably still worthwhile since the "self-terminating" nature of JSON seems like a really useful feature that is totally overlooked. – user371366 Jul 09 '16 at 20:42

0 Answers0