How can I get query answers back in JSON format?

When passing an arbitrary match query to TypeDB using Client Node.js, how can I get back a JSON view of the answers that represents them in a canonical format, including e.g:

  • whether they are entities, relations, attributes;
  • for attributes, include the value

The default return type of a match query is an Iterator of ConceptMap.

The answers retrieved from a match query can be any kind of Concept. Concepts are structured using a strict type hierarchy:


In order to obtain a canonical representation of an arbitrary Concept, we must check what subtype of Concept it is, and then deserialise accordingly.

function conceptMapsToJSON(conceptMaps) {
  return conceptMaps.map(x => conceptMapToJSON(x));
}
function conceptMapToJSON(conceptMap) {
  return Object.fromEntries(Array.from(conceptMap.map.entries()).map([varName, concept]) => [varName, conceptToJSON(concept)]));
}
function conceptToJSON(concept) {
  if (concept.isType()) return typeToJSON(type);
  else { throw "TODO: Not implemented yet"; }
}
function typeToJSON(type) {
  if (type.isEntityType()) return entityTypeToJSON(type);
  else { throw "TODO: Not implemented yet"; } // TODO: if (type.isRelationType()), etc.
}
function entityTypeToJSON(entityType) {
  return {
    label: entityType.label.name,
    encoding: "ENTITY_TYPE",
  }
}

These methods can be modified as appropriate to get the data in the exact format we want.