Schema-specific conditions for JSON data

I have been experimenting with the if-then-else statement in JSON, but I am encountering some issues with it.

{
"type": "object",
"minProperties": 2,
"maxProperties": 2,
"properties": {
    "human": {
        "enum": [
            "Kids",
            "Adults"
        ]
    }
},

"if": {
    "properties": {
        "human": {
            "const": "Adults"
        }
    },
"then": {
    "properties": {
        "shoe_size": {
            "type": "number",
            "minimum": 7,
            "maximum": 11
        }
    },
    "else": {
        "properties": {
            "shoe_size": {
                "type": "number",
                "minimum": 2,
                "maximum": 6
            }
        }
    }
},
"required": [
    "shoe_size",
    "human"
]
}

After applying this logic, the expected output should be:

{
 "human": "Adults",
 "shoe_size": 10
 }

However, I am noticing that the values for shoe sizes are not restricted within the minimum/maximum range as specified. Any insights on why this is happening would be greatly appreciated.

Thank you for your assistance.

Answer №1

It appears that there might be a nesting error in your code snippet. In order for the logic to work correctly, make sure that the else statement is on the same level as the if and then:

{
  "type": "object",
  "minProperties": 2,
  "maxProperties": 2,
  "properties": {
    "human": {
      "enum": [
        "Kids",
        "Adults"
      ]
    }
  },

  "if": {
    "properties": {
      "human": {
        "const": "Adults"
      }
    }
  },
  "then": {
    "properties": {
      "shoe_size": {
        "type": "number",
        "minimum": 7,
        "maximum": 11
      }
    }
  },
  "else": {
    "properties": {
      "shoe_size": {
        "type": "number",
        "minimum": 2,
        "maximum": 6
      }
    }
  },
  "required": [
    "shoe_size",
    "human"
  ]
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting Grouped Pandas DataFrames to JSON format

I am encountering some difficulties converting the given dataframe into a JSON structure. Despite my attempts, I haven't been able to complete the final step successfully. Here is the data frame I have: serialNumber | date | part | value | n ...

The functionality for tabbed content seems to be malfunctioning on Chrome and Firefox, however it works perfectly

In my index.js file, I have various functions set up like so: // a and b is placed at index.jsp $("#a").click(function (){ //this works on index.jsp and display.jsp(where the servlets forwards it). $("#b").load('servletA.html?action=dis ...

NodeJS JSONStream causing memory exhaustion issue

I've encountered an issue while trying to stream a large JSON file (94mb in size) from an HTTP request to a local file using the JSONStream library in NodeJS. Despite setting a memory flag of 256mb with node --max-old-space-size=256 .\main.js, th ...

Simply output the integer value

Recently, I've been working on a function that I'm trying to condense into a one-liner code for a challenge on Codewars. You can find the problem here. Here's the code snippet that I currently have: export class G964 { public static dig ...

Navigating the scope set by a specific string

Is there a more efficient method than iterating through child objects to access the value at a specific location in the $scope, identified by the attribute "Calendar.Scheduling.field.Appointment.ApptDateTime_Date"? I was hoping for a solution similar to an ...

Receiving an error while trying to install packages from the NPM registry due to non

I am facing some challenges while attempting to install my Ionic App through the registry along with its dependencies. I have been using npm i --loglevel verbose command, and my ~/.npmrc file is configured as follows: //nexus.OMMITED.com/repository/:_auth ...

Is your list rendering in Vue.js with AJAX ready to go?

At this moment, my Vue.js component is retrieving data from an Elasticsearch query and displaying it in a list like this: <li v-for="country in countries">{{ country.key }}</li> The issue I am facing is that I want to show users only a snippe ...

Activate the div when hovering over the span

Experiencing an issue with triggering a visible div while hovering over a span. Here is the code structure: <ul class="products"> <li> <a href="somelink"> <img src="some image"> <div class="overlay"> Some Text</div> & ...

Assistance needed for iterating through JSON data

I need assistance retrieving specific data from a JSON response in my JavaScript code. Unfortunately, the current approach is not providing the desired results: This is my JavaScript code snippet: function retrieveArtists(response){ var artistList ...

choosing between different options within Angular reactive forms

I am attempting to create a select element with one option for each item in my classes array. Here is the TypeScript file: @Component({ selector: 'app-create-deck', templateUrl: './create-deck.component.html', styleUrls: [' ...

Utilizing PHP to send arrays through AJAX and JSON

-I am facing a challenge with an array in my PHP file that contains nested arrays. I am trying to pass an integer variable (and may have to handle something different later on). -My objective is to make the PHP file return an array based on the incoming v ...

The URL for the Javascript chunk includes colons at https://example.com/js/chunk-vendors.b3792e11.js:18:16400

I recently completed a Vue application and used npm run build to generate the files. Upon uploading the dist folder to my Apache server, I encountered an issue where Apache was unable to locate the file when trying to access a specific part of the app (:18 ...

Which one should you begin with: AngularJS or Angular 2?

Interested in learning Angular and curious about the differences between Angular, AngularJS, and Angular 2. Should I focus on educating myself on Angular or go straight to Angular 2, considering it's now in beta version? Is there a significant differ ...

Rotate image in Vue3 using @click

I have a dashboard with a refresh button that I want to rotate 360 degrees every time it is clicked. How can I achieve this rotation effect on the image with each click of the refresh button? Below is the code snippet I have been working on, but it only r ...

InvalidArgument JSON

I encountered an issue while trying to store String[] in a jsonObject, resulting in the following error message: java.lang.IllegalArgumentException: Invalid type of value. Type: [[Ljava.lang.String;] with value: [[Ljava.lang.String;@189db56] at com.ibm. ...

Unable to resolve 500 error on Vercel in Next.js despite successful local development

Here is the content of route.ts import fs from 'fs'; import path from 'path'; import PizZip from 'pizzip'; import Docxtemplater from 'docxtemplater'; import { NextRequest, NextResponse } from 'next/server'; ...

Tips for organizing the output of wp_query

I'm looking to organize the results of my wp_query, wanting to arrange them by different parameters without needing to run the query again. Here is what I have so far: $the_query = new WP_Query( $args ); I’d like to sort $the_query; WP_Query gives ...

Incorporating a jQuery word count and limit within PHP code: a step-by-step guide

I am encountering an issue with a textarea count code. It functions perfectly on its own but when I integrate it as shown below, it stops working without throwing any errors. I have been trying to resolve this for more than 3 days now. Any assistance would ...

What is the best way to include a RecyclerView row in the WishList feature?

I am looking to incorporate a Wishlist feature similar to Google Play in my app. Each row in my RecyclerView within the QuestionActivity contains a Button (Heart Icon). My goal is for when this heart icon is clicked, the corresponding row in the Recycler ...

Creating multiple div elements with changing content dynamically

I am facing an issue with a div named 'red' on my website where user messages overflow the 40px length of the div. To prevent this, I want to duplicate the 'red' div every time a message is sent so that the messages stay within the boun ...