JSON Strings

JSON-formatted strings are used to send hardware descriptions and measurement results from your driver to FlexOTO. If your unfamiliar with the JSON format, you can find many tutorials on the internet.

In your driver, you can either directly create a string in JSON format or you can convert strings, variables, and data structures to a JSON string using a JSON library method such as json.dumps() in Python. The Python examples in this section demonstrate both of these methods for creating the identical JSON strings in your script. The scripts also validate the resulting JSON strings so that you can see that they both produce valid JSON string.

In your driver, you'll probably want to query the switch or instrument's serial number so that you can insert it into your JSON string.

JSON strings have these characteristics:

  • Unicode formatting which is native to Python 3.0 and above (UTF-8).
  • White space is legal for readability in JSON strings as are the newline ('\n'), the carriage return ('\r'), and Python's line continuation character ('\').
  • Multiple entries for switch "InputPorts" names, "OutputPorts" names, or instrument "Inputs" connectors names are entered as JSON arrays. If there is only one entry, you can list the element as either a single element array (["A"]) or as a name string ("A") without the brackets. Either method works.
  • JSON format errors or missing elements will cause your driver to fail when imported into FlexOTO.

Building and Validating JSON from a String (for Multiple Internal Switches)

This section describes switch models that have multiple internal switches. In the following script, notice that the required Groups element is a list that describes two internal switches. The Name elements provide the name of each internal switch.

In JSON strings, string elements must be enclosed in double quotes. Single quoted string elements will invalidate the JSON.

Copy
Validate JSON from String
import json

def valid_json(jsonstr):
    try:
        json.loads(jsonstr)
    except ValueError as e:
        return False
    return True

json_str = """
{
    "ModelNumber": "My Switch",
    "SerialNumber": "12455",
    "SettlingTimeSeconds": "50e-3",
    "Groups": [
        {
            "Name": "SW1",
            "InputPorts": ["1", "2", "3", "4", "5", "6", "7", "8"],
            "OutputPorts": ["IN"],
            "Wavelengths": ["1350 nm", "1550 nm"]
        },
        {
            "Name": "SW2",
            "InputPorts": ["1", "2", "3", "4", "5", "6", "7", "8", "9"],
            "OutputPorts": ["IN 1", "IN 2", "IN 3", "IN 4"],
            "Wavelengths": ["1350 nm", "1550 nm"]
        }
    ]
} """

print(valid_json(json_str))

Building and Validating JSON from Data Structure (for Multiple Internal Switches)

This section describes switch models that have multiple internal switches. In the following script, notice that the required Groups element is a list that describes two internal switches. The Name elements provide the name of each internal switch.

Strings, variables, and data structures in your code can use single or double quotes as allowed by the language. In this script, the json.dumps method converts single quote characters to double quotes.

Copy
Validate JSON from Structure
import json

def valid_json(jsonstr):
    try:
        json.loads(jsonstr)
    except ValueError as e:
        return False
    return True

sw1 = {'Name': 'SW1',
       'InputPorts': ['1', '2', '3', '4', '5', '6', '7', '8'],
       'OutputPorts': ['IN']}
sw2 = {'Name': 'SW2',
       'InputPorts': ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
       'OutputPorts': ['IN 1', 'IN 2', 'IN 3', 'IN 4'],
       'Wavelengths': ['1350 nm', '1550 nm']}
sw_list = [sw1, sw2]
data_structure = {'ModelNumber': 'My Switch', 'SerialNumber': '12455', 'SettlingTimeSeconds': '50e-3', 'Groups': sw_list}
json_str = json.dumps(data_structure)
print(valid_json(json_str))

Building and Validating JSON from a String (for One Internal Switch)

This section describes a switch model that has only one internal switch. In the following script, notice that the Groups element is required even though this switch model only has one internal switch. In this case, Groups is a list that contains a single item. Even though the internal switch does not have a name, the Name element is still required but is an empty string.

Copy
Validate JSON from String
import json

def valid_json(jsonstr):
    try:
        json.loads(jsonstr)
    except ValueError as e:
        return False
    return True

json_str = """
{
    "ModelNumber": "My Switch",
    "SerialNumber": "12455",
    "SettlingTimeSeconds": "50e-3",
    "Groups": [
        {
            "Name": "",
            "InputPorts": ["1", "2", "3", "4", "5", "6", "7", "8"],
            "OutputPorts": ["IN"],
            "Wavelengths": ["1350 nm", "1550 nm"]
        }
    ]
} """

print(valid_json(json_str))

In JSON strings, string elements must be enclosed in double quotes. Single quoted string elements will invalidate the JSON.

Building and Validating JSON from Data Structure (for One Internal Switch)

This section describes a switch model that has only one internal switch. In the following script, notice that the Groups element is required even though this switch model only has one internal switch. In this case, Groups is a list that contains a single item. Even though the internal switch does not have a name, the Name element is still required but is an empty string.

Strings, variables, and data structures in your code can use single or double quotes as allowed by the language. In this script, the json.dumps method converts single quote characters to double quotes.

Copy
Validate JSON from Structure
import json

def valid_json(jsonstr):
    try:
        json.loads(jsonstr)
    except ValueError as e:
        return False
    return True

switch = {'Name': '',
         'InputPorts': ['1', '2', '3', '4', '5', '6', '7', '8'],
         'OutputPorts': ['IN'],
         'Wavelengths': ['1350 nm', '1550 nm']}

sw_list = [switch]
data_structure = {'ModelNumber': 'My Switch', 'SerialNumber': '12455', 'SettlingTimeSeconds': '50e-3', 'Groups': sw_list}
json_str = json.dumps(data_structure)
print(valid_json(json_str))