Skip to main content

CSV Data

NQE queries can access data formatted as comma-seperated values (CSV).

Specifically, CSV data can be embedded within an NQE query using a multi-line CSV literal, as in:

deviceRegions =
"""csv
device,region,mgmtIp
core-01,nyc,112.2.3.1
core-02,nyc,112.2.4.1
edge-12,atl,112.3.3.1
""";

The CSV literal must start with the characters """csv followed by a newline, and is terminated by a newline followed by """. Within this region, the first non-empty line must contain the column headers, while every subsequent non-empty line is a data row. The number of column headers must be equal to the number of items in each row. The items in a row, as well as the header, must be separated by comma (,) characters, and newline characters must be used to terminate each row. Row items can be encapsulated with double-quotes (") in order to include an item that contains spaces, newlines, or commas.

The CSV data is represented in NQE as a list of records, where each record has fields corresponding to the columns of the CSV data. For example, the above CSV data is equivalent to the following:

deviceRegions =
[ { device: "core-01", region: "nyc", mgmtIp: ipAddress("112.2.3.1") },
{ device: "core-02", region: "nyc", mgmtIp: ipAddress("112.2.4.1") },
{ device: "edge-12", region: "atl", mgmtIp: ipAddress("112.3.3.1") }
];

Since the data is represented as NQE data, it can be queried like any other data. For example, the following selects the device name and ip for devices in the "nyc" region:

foreach entry in deviceRegions
where entry.region == "nyc"
select { deviceName: entry.device, ip: entry.mgmtIp }

NQE infers the type of each column in the CSV data. For example, in the above CSV, the device and region columns are treated as String type data, while the mgmtIp column is inferred to be IpAddress type data. More generally, if all values for a given column of the CSV data have a numeric format, then the column is interpreted as a Number type and the corresponding field of the NQE records has type Number. Similarly, if the values of a column are all formatted as IP addresses, IP subnets, MAC addresses, or boolean values (true or false, case-insensitive), respectively, then the column will be interpreted asIpAddress, IpSubnet, MacAddress, or Bool type, respectively. If a column's values contain a mixture of formats, then the column will be treated as String type.

caution

Note that the name of a column that is not a valid NQE identifier is mangled into one that is. For example, an NQE identifier cannot contain a dash, so in the following CSV data, the column name mgmt-ip is mangled to mgmt_ip.

deviceRegions =
"""csv
device,region,mgmt-ip
core-01,nyc,112.2.3.1
edge-12,atl,112.2.4.1
""";