Skip to content

Is there a way to validate the structure of a json object ? #604

Closed

Description

@Oberonc

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_* ?

Activity

  1. pboettch commented on Jun 3, 2017

    @pboettch
    Contributor

    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.

  2. nlohmann commented on Jun 4, 2017

    @nlohmann
    Owner

    @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.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions