Skip to content
check-square

GitHub Action

JSON string/file validator

v1.0.0 Latest version

JSON string/file validator

check-square

JSON string/file validator

Checks if supplied input string/file is valid json

Installation

Copy and paste the following snippet into your .yml file.

              

- name: JSON string/file validator

uses: tonys-code-base/[email protected]

Learn more about this action in tonys-code-base/json-check-action

Choose a version

Check if string/file contents are valid JSON

Overview

Basic action to validate if input file contents, or input string are valid JSON.

Usage

Check if file content is valid JSON

Possible use case might be to validate configuration updates/edits to a JSON file when a PR is raised.

Example when input file contains valid JSON

Filename (all valid JSON): ${{ github.workspace }}/config-file.json

{
    "example_meta": [
        {
            "meta_no": "57",
            "meta_status": "active",
            "meta_suburb": {
                "name": "random_suburb",
                "zip": "012012"
            }
        }
    ]
}

Include step to validate contents of file ${{ github.workspace }}/config-file.json.

...
...
on:
  pull_request:
...

jobs:
  ...
  ...
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: JSON string/file validator
        uses: tonys-code-base/[email protected]
        id: json-file-chk
        with:
          json-file-path: ${{ github.workspace }}/config-file.json

output:

Run tonys-code-base/[email protected]
  with:
    json-file-path: /runner/_work/***/***/config-file.json

Successfully parsed JSON file: 
/runner/_work/***/***/config-file.json

When input file contains invalid JSON

Same file but with invalid JSON: ${{ github.workspace }}/config-file.json

{
    "example_meta": [
        {
            "meta_no": "57",
            "meta_status": "active",
            "meta_suburb": {
                "name": "random_suburb",
            }
        }
    ]
}

sample workflow output:

Run tonys-code-base/[email protected]
  with:
    json-file-path: /runner/_work/***/***/config-file.json

parse error: Expected another key-value pair at line 8, column 13
Error: Process completed with exit code 4.

Using the action to validate JSON strings

...
...

jobs:
  ...
  ...
    steps:
      - name: JSON string/file validator
        uses: tonys-code-base/[email protected]
        id: json-str-chk
        with:
          json-string: >-
            {
              "example_meta": [
                  {
                      "meta_no": "57",
                      "meta_status": "active",
                      "meta_suburb": {
                          "name": "random_suburb",
                          "zip": "012012"
                      }
                  }
              ]
            }

Output:

0s
Run tonys-code-base/[email protected]

Successfully parsed JSON string: 
{
  "example_meta": [
      {
          "meta_no": "57",
          "meta_status": "active",
          "meta_suburb": {
              "name": "random_suburb",
              "zip": "012012"
          }
      }
  ]
}