colanderjs
Declarative JSON parser.
Most payloads aren't exactly what you need for your application, but manual parser functions can be large, tricky to write, and brittle.
colanderjs
is a declarative solution loosely inspired by GraphQL that makes it trivial to reshape a JSON object into exactly what you need. The shape you define is the shape you get:
;;; const parseWorksByAuthor = ; ;// {// author: {// name: 'J. R. R. Tolkien',// pseudonym: 'Oxymore',// works: [// {// title: 'The Hobbit',// summary: 'In a hole in the ground there lived a hobbit.'// },// ...// ]// }// }
The original payload root
is passed as a second argument to c
in case you need to refer back to information from original json:
;; const parseAuthors = ; ;// {// author: {// name: 'J. R. R. Tolkien',// group: {// name: 'Inklings'// }// }// }
APIs often return a payload you need to traverse prior to parsing. You can pass an extractor function as the first argument to the top-level colander
, which also sets root
to the result of the extraction:
const parseAuthors = ;
API
colander
Top-level alias for c
.
fullName: 'Joshua Martin' memberOf: 'nerds' ; fullName: 'Joshua Martin' memberOf: 'nerds' fullName: 'John Doe' memberOf: 'geeks' ; fullName: 'Joshua Martin' memberOf: 'nerds' ;
c
name: 'Joshua Martin' ; user: name: 'Joshua Martin' random: 'yeet' ; users: name: 'Joshua Martin' name: 'John Doe' ; name: 'Joshua Martin' name: 'John Doe' ;
CommonJS
const colander c = ;