Skip to main content

Parse JSON file to API

The below content is from the quickstart tutorial.

01 - JSON output to API

The most easiest way for you to convert an existing command or script to API is if it outputs to a supported output such as JSON.

Simply supply your required run command, view all the available options here.

Prepare sample data

  1. Ready an existing JSON file or script that outputs JSON, else save an example JSON file eg.
heroes.json
{
"message": "hero ouput",
"heroes": [
{
"name": "batman",
"identity": "bruce"
},
{
"name": "superman",
"identity": "clark"
}
]
}

Review configuration

  1. Set the interface name and/or http route on how you want to access the output
  2. Specify the command you'd like to run
  3. Set the parse option to json
  4. Place the configuration into your configs directory
  5. Stop/Start the binary
./airpipe --api-key enter-your-api-key --config-dir configs/
tutorial-json-cat-to-api.yml
name: json-cat-to-api
description: run cat command to read json file

interfaces:
tutorial/json-cat-to-api:
output: http # as we have not specified a method it will default to a GET route

actions:
- name: RunCommand
command:
run: cat heroes.json # this can be any command or script that outputs json
parse:
data_type: json # this will ensure it attempts to process the data as json

Test

As we've specified http we can just run this in our browser or a perform a simple curl

http://0.0.0.0:4111/tutorial/json-cat-to-api

HTTP Response

{
"data": {
"RunCommand": {
"data": {
"heroes": [
{
"identity": "bruce",
"name": "batman"
},
{
"identity": "clark",
"name": "superman"
}
],
"message": "hero ouput"
},
"time.ms": 1
}
}
}