Skip to content

JSON Array of length 1 gets compacted into non array #509

@adlerfaulkner

Description

@adlerfaulkner

When compacting (or framing with an @context), any @value of a node with "@type': "@json" that is a JSON array of length 1 gets transformed into a non array value equalling the first item in the JSON array.

For example:

const doc = {
  "https://example.com/people": {
    "@type": "@json",
    "@value": [ { "name": "Jim" } ]
  }
};

const context = {
  "people": {
    "@id": "https://example.com/people",
    "@type": "@json"
  }
};

const compacted = await jsonld.compact(doc, context);
console.log(JSON.stringify(compacted, null, 2));
/* Output:
{
  "@context": {
    "people": {
      "@id": "https://example.com/people",
      "@type": "@json"
    }
  },
  "people": {
    "name": "Jim"
  }
}
*/

Note above that the people field in the output was transformed from a JSON array to an object equal the the original array's first value.

I would expect this library to preserve the original JSON array.
The output should be:

{
  "@context": {
    "people": {
      "@id": "https://example.com/people",
      "@type": "@json"
    }
  },
  "people": [
    { "name": "Jim" }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions