I am parsing many JSONs and DOMs in PHP. The problem is that the my knowledge about the structure of the JSON/DOM is mixed with code. For example, suppose that I have few sources of first and last names. Each data source can look different and have its own tricks, for example:

  1. [
      {"firstname": "FirstName1", "lastname": "LastName1"},
      {"firstname": "FirstName2", "lastname": "LastName2"}
  2. {
      "firstnames": ["FirstName1", "FirstName2"],
      "lastnames": ["LastName1", "LastName2"]
  3. {
        {"firstname": "FirstName1", "id": 1},
        {"firstname": "FirstName2", "id": 2}
        {"lastname": "LastName2", "id": 2},
        {"lastname": "LastName1", "id": 1}

So for each data source I write code that turns the JSON into my objects. If the JSON is very complex then the code also is very complex and it is impossible to see the JSON structure in my code. Looking for bugs is very hard.

I want to solve the problem in declarative way, something like a JSON regex where I can tell "first names are in 'firstnames', second names are in 'secondnames', and they are linked by 'id'".

How can I achieve that? Does JSON have some kind of declarative transformation language (like XSLT/XPath for XML)? Are there any strategies or patterns that can be used to make this JSON → Model transformation more declarative?

  • 1
    I edited out the part about tool/libary recommendations, but the rest of the question is absolutely interesting and valuable. I'm voting to reopen.
    – amon
    Nov 16 '15 at 10:54
  • 1
    While tool recommendations are off topic, JSONPath is a thing that exists, and is explicitly intended to be "XPath for JSON". You might find it useful.
    – Ixrec
    Dec 20 '15 at 15:21

So, you are trying to invent a DSL to navigate JSON, right? Something like XML's XPath.

Well, what JSON document represents is actually a tree or a multidimensional multidimensional multi... well, you got me... array / list (PHP arrays are lists also).

So, the very simple JSON DSL parser may look like this:

  1. Convert input JSON into array / list.
  2. Take DSL like "firstnames.innerfolder.0" and parse it the following way:
    1. Split by .
    2. Foreach part of the DSL try to dive into array element with the given name

When you reach the end of DSL, that will be the target element, containing what you've found.


Without recommending a specific tool or lib (since it's considered off topic), I would definitely recommend you to use the reference JSON parser for your language and try, if you can't normalize your inputs, to write specific code for each format.

In my humble opinion, parsing it by hand and/or trying to reuse code in each use case is likely to be worse in terms of readability, maintainability, or even security.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.