Skip to content

Inconsistent result from single object to multiple objects of same element #2

Description

@pointybeard

The resultant JSON differs depending if there is a single object or multiple of the same object.When there is only a single object (e.g. there is only 1 entry from a data-source), the result is a single object. However, when there is more than object of the same element (more than just 1 result from a data-source) the resultant JSON is an array.

Steps to reproduce

  1. Create a simple XML output with just a single entry
  2. Observe the JSON output
  3. Add another entry to the same output
  4. Observe the JSON again

Actual output

From XML like this:

<data>
  <entries>
    <entry>
      <id>2</id>
      <title>Another Entry</title>
      <handle>another-entry</handle>
      <body>Blah Blah</body>
      <publish-date>
        <date>2016-04-25</date>
        <time>19:03</time>
      </publish-date>
    </entry>
    <entry>
      <id>1</id>
      <title>An Entry</title>
      <handle>an-entry</handle>
      <body>This is a dummy entry</body>
      <publish-date>
        <date>2016-04-25</date>
        <time>16:53</time>
      </publish-date>
    </entry>
  </entries>
</data>

We get JSON like this:

{
    "entries": {
        "entry": [
            {
                "id": "2",
                "title": "Another Entry",
                "handle": "another-entry",
                "body": "Blah Blah",
                "publish-date": {
                    "date": "2016-04-25",
                    "time": "19:03"
                }
            },
            {
                "id": "1",
                "title": "An Entry",
                "handle": "an-entry",
                "body": "This is a dummy entry",
                "publish-date": {
                    "date": "2016-04-25",
                    "time": "16:53"
                }
            }
        ]
    }
}

However, remove 1 entry (so only 1 remains) and the output looks like this:

{
    "entries": {
        "entry": {
            "id": "1",
            "title": "An Entry",
            "handle": "an-entry",
            "body": "This is a dummy entry",
            "publish-date": {
                "date": "2016-04-25",
                "time": "16:53"
            }
        }
    }
}

Notice that the second example is not a JSON array.

Expected output

In both examples, it should be an array.

Possible solutions

It is possible the first behaviour is desirable since there will only ever be a single entry, this an array is unnecessary. One solution might be to have attributes on the XML e.g. forceArray="true" or similar which tells the JSON handler to behave in a certain way.

A stop-gap solution is to always include an empty element which tricks the parser into thinking it's an array. e.g.

<data>
  <entries>
    <entry>
      <id>2</id>
      <title>Another Entry</title>
      <handle>another-entry</handle>
      <body>Blah Blah</body>
      <publish-date>
        <date>2016-04-25</date>
        <time>19:03</time>
      </publish-date>
    </entry>
    <entry></entry>
  </entries>
</data>
    "entries": {
        "entry": [
            {},
            {
                "id": "1",
                "title": "An Entry",
                "handle": "an-entry",
                "body": "This is a dummy entry",
                "publish-date": {
                    "date": "2016-04-25",
                    "time": "16:53"
                }
            }
        ]
    },

However, this is not ideal.

Activity

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

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions