JSON - Validate object

Octopus.Script exported 2022-06-02 by cramoscr belongs to ‘JSON’ category.

Validates the consistency of an input JSON object against provided JSON schema

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

JSON input object

vJsonData =

JSON object to be validates

Ejem:

{
   "name" : "John",
   "age" : 45
}

Schema

vSchema =

JSON object called schema, who defines the layout of the JSON input object

{
   "type":"object",
   "properties":{
      "name":{
         "type":"string",
         "minimum":1
      },
      "age":{
         "type":"number",
         "minimum":1
      }
   },
   "additionalProperties":false,
   "required":[
      "name",
      "age"
   ]
}

Script body

Steps based on this template will execute the following Python script.

import json
from jsonschema import validate

def validateJSON(jsonData):
    try:
        #jsonData must be STRING type
        json.loads(jsonData)
    except Exception as e:
        #print(str(e))
        #print(e.message)
        return str(e)
    return None

def validateSchema(jsonData, jsonSchema):
    try:
        validate(instance=json.loads(jsonData), schema=json.loads(jsonSchema))
    except Exception as e:
        #print(e.message)
        #print(str(e))
        return e.message
    return None

vSchema = get_octopusvariable("vSchema")
vJsonData = get_octopusvariable("vJsonData")

vError = validateJSON(vJsonData)
if vError == None:
   vRslt = 'Correct!'
   print('JSON Structure is valid !','\n')

   if vSchema:
      vError = validateSchema(vJsonData, vSchema)
      if vError == None:
         vRslt = 'Correct!'
         print('JSON Schema is valid !','\n')
      else:
         vRslt = 'Wrong !'        
         print('JSON Schema error:', vError, file=sys.stderr)
else:
    vRslt = 'Wrong!'
    print('JSON structure error:', vError, file=sys.stderr)

print ('Result:', vRslt)

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "7f4f6d86-50aa-43b1-be7b-f0ab27e4a0ac",
  "Name": "JSON - Validate object",
  "Description": "Validates the consistency of an input JSON object against provided JSON schema",
  "Version": 1,
  "ExportedAt": "2022-06-02T19:54:24.484Z",
  "ActionType": "Octopus.Script",
  "Author": "cramoscr",
  "Packages": [],
  "Parameters": [
    {
      "Id": "1905df72-1da7-446b-8857-4df1795fb19a",
      "Name": "vJsonData",
      "Label": "JSON input object",
      "HelpText": "JSON object to be validates\n\nEjem:\n\n```\n{\n   \"name\" : \"John\",\n   \"age\" : 45\n}\n```",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      }
    },
    {
      "Id": "3089fe56-964f-4d5f-b940-31ca81e358cb",
      "Name": "vSchema",
      "Label": "Schema",
      "HelpText": "JSON object called schema, who defines the layout of the JSON input object\n\n```\n{\n   \"type\":\"object\",\n   \"properties\":{\n      \"name\":{\n         \"type\":\"string\",\n         \"minimum\":1\n      },\n      \"age\":{\n         \"type\":\"number\",\n         \"minimum\":1\n      }\n   },\n   \"additionalProperties\":false,\n   \"required\":[\n      \"name\",\n      \"age\"\n   ]\n}\n```",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "Python",
    "Octopus.Action.Script.ScriptBody": "import json\nfrom jsonschema import validate\n\ndef validateJSON(jsonData):\n    try:\n        #jsonData must be STRING type\n        json.loads(jsonData)\n    except Exception as e:\n        #print(str(e))\n        #print(e.message)\n        return str(e)\n    return None\n\ndef validateSchema(jsonData, jsonSchema):\n    try:\n        validate(instance=json.loads(jsonData), schema=json.loads(jsonSchema))\n    except Exception as e:\n        #print(e.message)\n        #print(str(e))\n        return e.message\n    return None\n\nvSchema = get_octopusvariable(\"vSchema\")\nvJsonData = get_octopusvariable(\"vJsonData\")\n\nvError = validateJSON(vJsonData)\nif vError == None:\n   vRslt = 'Correct!'\n   print('JSON Structure is valid !','\\n')\n\n   if vSchema:\n      vError = validateSchema(vJsonData, vSchema)\n      if vError == None:\n         vRslt = 'Correct!'\n         print('JSON Schema is valid !','\\n')\n      else:\n         vRslt = 'Wrong !'        \n         print('JSON Schema error:', vError, file=sys.stderr)\nelse:\n    vRslt = 'Wrong!'\n    print('JSON structure error:', vError, file=sys.stderr)\n\nprint ('Result:', vRslt)"
  },
  "Category": "JSON",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/json-validate-format-and-schema.json",
  "Website": "/step-templates/7f4f6d86-50aa-43b1-be7b-f0ab27e4a0ac",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAzjSURBVHhe7Z17cFTVHceRh0mJkAwWNoOYDdYxWDAJjzxgJNHpAKFTGhi10inysHXAFqbWGZlpnVF0xn/amVZHWmGoGkBHrFCIdkrSVF5tAYMdA4pm8ZWNdkxKjdnNKMFkoF+zv2y3e+9ubrL37N5z7/czO8P53bA3m93P/Z3fOefeu1dcvnx5FCF2M1r+JcRWKBZRAsUiSqBYRAkUiyiBYhElUCyiBIpFlECxiBIoFlECxSJKoFhECRSLKIFiESVQLKIEikWUQLGIEigWUQLFIkqgWEQJFIsowdFX6XR3h9qCwe7uboldTXZ2dqHfn5/vk1hzHCpWb2/vgfpXAoFzEnsGv79g5Z13QDKJtcWhYtXt3B0MtkvgMbKystauuUv31OXEGqvl9BnPWgUuXryI46qjo1NiPXGiWG1tQWl5FRe45USxPFKtJ0d3tzjd4Fy0dotiORp93aJYTkdTtyiWBujolhPnsfAmJpluKCkpzsvLlUBnjh79m7Ssodf8ln5irVm9qrDQL4HOPPLoY9KyjEZusSvUCY36RIqlGbq4RbH0Qwu3KJaWON8tiqUrDneLYmmMk92iWHrjWLcolvY40y2K5QYc6BbFcglOc4tiuQdHuUWxXIVz3KJYbsMhblEsF+IEtyiWO8m4WxTLtWTWLYrlZjLoFsVyOZlyi2K5n4y4RbE8QfrdolheIc1uUSwPkU63KJa3SJtbFMtzpMctiuVF0uAWxfIoqt2iWN5FqVsUy9Ooc4tieZ2IW729vRLbBMUiX7m158WXJLAJikW+Ihhst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iESVQLKIEikWUQLGIEigWUQLFIkqgWEQJFIsogWIRJVAsogSKRZRAsYgSKBZRAsUiStDvRD+iCJ7oRzSAYhElUCyiBIpFlECxiGDvRXVOFCttF3CSWPLz86VlB44Uq6iIN6tJM35/QV6enXdjdKJY+AtrahbTrbTh8/ls/0JhJ06QRujuDh05eqytLWjjN46QOJCoCgv9lRXltl/y71yxiNZwVEiUQLGIEigWUQLFIkqgWEQJFIsogWIRJVAsogSKRZRAsYgSKBZRAsUiSqBYRAkUiyiBYhElUCyiBIpFlECxiBIoFlECxSJKoFhEAaNG/Rcu9qur8BubCgAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, June 2, 2022