VerifyJSON

Verifies if the supplied JSON string is valid and optionally verifies against a provided schema. The script utilizes Powershell's Test-JSON cmdlet.

powershell · Common Scripts

Details

IDVerifyJSON
Languagepowershell
From Version5.5.0
Docker Imagedemisto/powershell:7.5.0.9017890
TagsJSON Utility

README

Verifies if the supplied JSON string is valid and optionally verifies against a provided schema. The script utilizes Powershell’s Test-JSON cmdlet.

Script Data


Name Description
Script Type powershell
Tags JSON, Utility
Cortex XSOAR Version 5.5.0

Inputs


Argument Name Description
json JSON string to verfiy.
schema Optional schema against which to validate the JSON input.

Outputs


Path Description Type
VerifyJSON.Result Whether the passed JSON was verified. boolean

Script Example

!VerifyJSON json={"alert_id":"695b3238-05d6-4934-86f5-9fff3201aeb0"}

Context Example

{
    "VerifyJSON": {
        "Result": true
    }
}

Human Readable Output

Verify JSON completed successfully

BeforeAll {
    . $PSScriptRoot\VerifyJSON.ps1
}

Describe 'VerifJSON' {
    Context "Valid Json" {
        BeforeAll {
            Mock ReturnOutputs {}
        }        
        It 'Check Valid Json passes' {
            $demisto.ContextArgs = @{json = '{"test": "this"}' }
            Main
            Assert-MockCalled -CommandName ReturnOutputs -Times 1
        }
    }

    Context "InValid Json" {
        BeforeAll {
            Mock ReturnError {}
        }        
        It 'Check InValid Json fails' {
            $demisto.ContextArgs = @{json = '{"test": this"}' }
            Main
            Assert-MockCalled -CommandName ReturnError -Times 1 -ParameterFilter {$Message.Contains("Cannot parse the JSON")}
        }
    }

    Context "Schema Validation" {
        BeforeAll {
            Mock ReturnError {}
        }
        
        It 'Check InValid Json schema fails' {
            $schema = @'
{
    "definitions": {},
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://example.com/root.json",
    "type": "object",
    "title": "The Root Schema",
    "required": [
    "name",
    "age"
    ],
    "properties": {
    "name": {
        "$id": "#/properties/name",
        "type": "string",
        "title": "The Name Schema",
        "default": "",
        "examples": [
        "Ashley"
        ],
        "pattern": "^(.*)$"
    },
    "age": {
        "$id": "#/properties/age",
        "type": "integer",
        "title": "The Age Schema",
        "default": 0,
        "examples": [
        25
        ]
    }
    }
}
'@
            $demisto.ContextArgs = @{json = '{"name": "Ashley", "age": "25"}'; schema = $schema}
            Main
            Assert-MockCalled -CommandName ReturnError -Times 1 -ParameterFilter {$Message.Contains("not valid with the schema")}
        }
    }
}