Skip to content

Preprocess JSON to name and value keys for Morpheus

License

Notifications You must be signed in to change notification settings

spoonboy-io/switch

Repository files navigation

Switch

Preprocess JSON to name and value keys for Morpheus

GitHub go.mod Go version Go Report Card GitHub Workflow Status GitHub Workflow Status (branch)  GitHub Release Date GitHub commits since latest release (by date) GitHub

About

Switch performs preprocessing on JSON data. Designed to simplify Morpheus option list creation and management, Switch can fetch a target URL and parse it, creating name and value keys in a new JSON file according to the YAML configuration rules provided.

Switch can manage multiple source files, and, using a Time to Live (TTL) for each source it will refresh the data at the specified interval.

Switch saves the data as a new JSON file in a location you specify. Any web server can serve it, you could also use the Dujour JSON/CSV data file server.

Why?

Formulating the JavaScript for Morpheus Option List REST translation scripts can be complex, while any JSON file which presents as an array of name and value keys, needs no translation script whatsoever since Morpheus is able to interpret it automatically.

So, Switch takes a complex JSON payload, which would require a translation script to parse in Morpheus, and creates a simple representation of the data needed for the option list.

Basic use, requires no translation script, more complicated use (such as dependent inputs) will mean any translation script is much simplified.

Releases

You can find the latest software here.

Usage

Switch will look for and parse a sources.yaml file which has the following format:

---
- source:
    description: Some blog posts (array)
    url: https://jsonplaceholder.typicode.com/posts
    token: Bearer xxxxx-xxx-xxx
    extract:
      root:
      name: title
      value: id
    ttl: 5 # minutes
    save:
      folder: test
      filename: test.json

The above configuration creates this file ./test/test.json locally:

[{
	"name": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
	"value": 1
}, {
	"name": "qui est esse",
	"value": 2
}, {
	"name": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
	"value": 3
}, {
	"name": "eum et est occaecati",
	"value": 4
}, {
	"name": "nesciunt quas odio",
	"value": 5
},
  
  // ... abbreviated
  
  {
	"name": "at nam consequatur ea labore ea harum",
	"value": 100
}]

A similar config, which adds a root of "batter", can parse remote JSON like this:

{
  "items": {
    "item": [
      {
        "batters": {
          "batter": [
            {
              "id": "1001",
              "type": "Regular"
            },
            {
              "id": "1002",
              "type": "Chocolate"
            },
            {
              "id": "1003",
              "type": "Blueberry"
            },
            {
              "id": "1004",
              "type": "Devil's Food"
            }
          ]
        },
        "id": "0001",
        "name": "Cake",
        "ppu": 0.55,
        "topping": [
          {
            "id": "5001",
            "type": "None"
          },
          {
            "id": "5002",
            "type": "Glazed"
          },
          {
            "id": "5005",
            "type": "Sugar"
          },
          {
            "id": "5007",
            "type": "Powdered Sugar"
          },
          {
            "id": "5006",
            "type": "Chocolate with Sprinkles"
          },
          {
            "id": "5003",
            "type": "Chocolate"
          },
          {
            "id": "5004",
            "type": "Maple"
          }
        ],
        "type": "donut"
      }
    ]
  }
}

To this:

[{
	"name": "Regular",
	"value": "1001"
}, {
	"name": "Chocolate",
	"value": "1002"
}, {
	"name": "Blueberry",
	"value": "1003"
}, {
	"name": "Devil's Food",
	"value": "1004"
}]

Which avoids the need to use any translation script, which would otherwise look like this if parsing the original file:

for (var x = 0; x < data.items.item[0].batters.batter.length; x++) {
  results.push({
    name: data.items.item[0].batters.batter[x].type, 
    value: data.items.item[0].batters.batter[x].id
  });
}

Installation

Grab the tar.gz or zip archive for your OS from the releases page.

Unpack it to the target host, and then start the application.

./switch

Or with debug output, which will show retrieved source, parsed output and heap allocated, in addition to normal log and error reporting..

./switch -debug

Or with nohup..

nohup ./switch &

TODO

  • Unit tests only cover the extraction routines
  • Only manually tested with unauthenticated GET requests at this time
  • The extraction code, caters for simple arrays and objects keys which store an array

License

Licensed under Mozilla Public License 2.0