Non Json

Input and output format

A how-to guide for working with non-JSON input and output for apps.

Within the Nextmv Cloud, the API expects JSON input and output. If you want to work with a format for input/output that is not JSON, you can use JSON as a wrapper for non-JSON input and output.

As an example, we'll walk through how to use CSV as the non-JSON input and output format. The same approach can be used for other formats.

Input

We take a CSV input and encode it as a base64 string. We then create a JSON object with a data key. The value of the data key is the base64-encoded string. That input JSON can then be sent to the Nextmv cloud.

Sample CSV input

order_id,customer_id,order_due,order_value
1,1,2021-01-01,100
2,2,2021-01-02,200
3,3,2021-01-03,300
4,4,2021-01-04,400
Copy

Convert to JSON

To automatically convert your CSV input you would write code to transform it to the structure outlined in the beginning. We provide some code snippets below to help you get started.

jq -n --arg data "$( cat input.csv | base64 )" '{"data": $data}'
Copy

Now the original CSV input is wrapped in a JSON object, so we can submit it to the Nextmv cloud and let your custom application handle the input.

{
  "data": "Im9yZGVyX2lkIiwiY3VzdG9tZXJfaWQiLCJvcmRlcl9kdWUiLCJvcmRlcl92YWx1ZSIKIjEiLCIxIiwiMjAyMS0wMS0wMSIsIjEwMCIKIjIiLCIyIiwiMjAyMS0wMS0wMiIsIjIwMCIKIjMiLCIzIiwiMjAyMS0wMS0wMyIsIjMwMCIKIjQiLCI0IiwiMjAyMS0wMS0wNCIsIjQwMCI="
}
Copy

Output

Define the output format

If you want to also emit non-JSON output, you can use the same approach using the data key. In your custom application you'll have to write code that encodes the non-JSON output as a base64 string.

In addition, you can add statistics information to the JSON object. The statistics key has to follow the statistics convention and it allows you to experiment with your application.

The output would then look like the snippets below.

{
  "data": "Im9yZGVyX2lkIiwiY3VzdG9tZXJfaWQiLCJvcmRlcl9kdWUiLCJvcmRlcl92YWx1ZSIKIjEiLCIxIiwiMjAyMS0wMS0wMSIsIjEwMCIKIjIiLCIyIiwiMjAyMS0wMS0wMiIsIjIwMCIKIjMiLCIzIiwiMjAyMS0wMS0wMyIsIjMwMCIKIjQiLCI0IiwiMjAyMS0wMS0wNCIsIjQwMCI=",
  "statistics": {
    "schema": "v1",
    "result": {
      "duration": 0.000368916,
      "value": 27
    },
    "run": {
      "duration": 0.000368916
    }
  }
}
Copy

In case you want to follow the Nextmv Go SDK runner convention, you can return an array of solutions instead of a single solution.

{
  "solutions": [
    {
      "data": "Im9yZGVyX2lkIiwiY3VzdG9tZXJfaWQiLCJvcmRlcl9kdWUiLCJvcmRlcl92YWx1ZSIKIjEiLCIxIiwiMjAyMS0wMS0wMSIsIjEwMCIKIjIiLCIyIiwiMjAyMS0wMS0wMiIsIjIwMCIKIjMiLCIzIiwiMjAyMS0wMS0wMyIsIjMwMCIKIjQiLCI0IiwiMjAyMS0wMS0wNCIsIjQwMCI=",
    }
  ],
  "statistics": {
    "schema": "v1",
    "result": {
      "duration": 0.000368916,
      "value": 27
    },
    "run": {
      "duration": 0.000368916
    }
  }
}
Copy

Decode the output after the run

When pulling the results from Nextmv Cloud you will have to decode the base64 encoded data similiar to what we have done for encoding the input. Below are code snippets to help you get started more quickly. They operate on an output with a data key in the root object.

jq -r '.data' output.json | base64 --decode
Copy

Page last updated

Go to on-page nav menu