You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Is there a way to validate the structure of a json object ? #604
Suppose I parsed a string from an unreliable source.
How can I validate the result json object ? (make sure it has certain structure / contents. Such as a list of 3 numbers and then a string ?)
Other than using a long boilerplate code with size() and get<>() and is_* ?
To validate a json instance or document, the json-schema.org has defined "a vocabulary that allows you to annotate and validate JSON documents" (citation from their site).
To my knowledge, there are two libraries written in C++ on github. One written by me, which supports draft4-version of json-schema.org only and is entirely based on this (nlohmann/json) library for json-parsing.
And there is valijson by Tristan Penman which does support other C++ json-parsers as well.
Activity
pboettch commentedon Jun 3, 2017
To validate a json instance or document, the json-schema.org has defined "a vocabulary that allows you to annotate and validate JSON documents" (citation from their site).
To my knowledge, there are two libraries written in C++ on github. One written by me, which supports draft4-version of json-schema.org only and is entirely based on this (nlohmann/json) library for json-parsing.
And there is valijson by Tristan Penman which does support other C++ json-parsers as well.
nlohmann commentedon Jun 4, 2017
@pboettch is right - JSON Schema is the tool to use here. It lets you specify your requirements with a JSON document and avoids manual checks.