0

I'm writing a JSON schema structure to validate an output JSON from various API's I'm using.

I have the following JSON structure:

"Stocks": {
     "description": "some stock data",
     "type": "array",
     "items: {
        "type": "object",
        "properties": {
            "unknown_property1": {
               "type": "object",
               "properties": {
                  "value":{"type":"integer"},
                  "ticker":{"type":"string"}
                  }
             },
            "unknown_property2": {
               "type": "object",
               "properties": {
                  "value":{"type":"integer"},
                  "ticker":{"type":"string"}
                  }
             }
         }
     }
 }

In place of the unknown properties, I'd like to have a method for introducing variation to those values. They are always going to be string.

I've seen this post, though it stops before getting to the nesting-level I'm interested in.

How can I leave unknown_property(1/2) ambiguous while ensuring that their values are objects?

Thank you.

  • Welcome to Stack Overflow. Is there a specific number of these "unknown properties"? Are there any rules (beyond what JSON demands) for the property names? Do their values all have the structure that you show here? – Karl Knechtel Jun 22 '22 at 19:30
  • "I've seen this post, though it stops before getting to the nesting-level I'm interested in." I don't understand why this doesn't answer the question. The application seems direct to me. – Karl Knechtel Jun 22 '22 at 19:35
  • Thanks @KarlKnechtel! There are no rules beyond what JSON demands for the property names and the values all have the same structure. – Sim Borodach Jun 22 '22 at 21:01
  • I am not seeing from the post that asks this same question how the solution applies to cases where the top level is itself an object. More specifically, how can it be that a specific name is absent for a property in an object? How would this be written? Moreover, the sub-properties I'd like to use are objects themselves. – Sim Borodach Jun 22 '22 at 21:16
  • 1
    "I am not seeing from the post that asks this same question how the solution applies to cases where the top level is itself an object." First off, I don't understand why this should cause a problem. "The top level" **normally** is an object, and specifically is in that question. Second, as far as I can tell, **the schema in the other question is structured the same way as yours**. You have a top-level entity (yours is named `"Stocks"` while the other is named `"meta"`; that entity is an *array, not an object*; its items are objects; the objects need to have "unknown properties". – Karl Knechtel Jun 22 '22 at 21:28
  • Therefore, the solution is the same: instead of specifying `"properties"` of the objects that have unknown properties, specify `"patternProperties"`; use a regular expression for the unknown-property name; then use a schema to describe the value that will correspond to that name . – Karl Knechtel Jun 22 '22 at 21:30
  • "Moreover, the sub-properties I'd like to use are objects themselves." So, see where it says `"type": "string"` in the question and answer? That's where you put the specification for the object instead, since that's the part that explains what the sub-properties are, and your sub-property is an object instead of a string. – Karl Knechtel Jun 22 '22 at 21:31
  • "how can it be that a specific name is absent for a property in an object? How would this be written?" *In the way that is explained in the answer to that question*: by using `patternProperties` to describe a *pattern* that the *property* names have, instead of specifically naming properties. – Karl Knechtel Jun 22 '22 at 21:32
  • @KarlKnechtel Thank you for explaining this line by line. I see all that needs to be done after using patternProperties is replacing "type": "string" to "type": "object" and then specifying the properties of that object. Thanks. – Sim Borodach Jun 23 '22 at 13:36

0 Answers0