Table of Contents

Prototype: readdata(filename, filetype)

Return type: data

Description: Parses CSV, JSON, or YAML data from file filename and returns the result as a data variable.

When filetype is auto, the file type is guessed from the extension (ignoring case): .csv means CSV; .json means JSON; .yaml or .yml means YAML. If the file doesn't match any of those names, JSON is used.

When filetype is CSV,JSON,YAML or ENV, this function behaves like readcsv(), readjson(), readyaml() or readenvfile() respectively. These functions have an optional parameter maxbytes (default: inf). maxbytes can not be set using readdata(), if needed use one of the mentioned functions instead.


  • filename: string - File name to read - in the range: "?(/.*)
  • filetype: - Type of data to read - one of
    • CSV
    • YAML
    • JSON
    • ENV
    • auto



echo -n 1,2,3 > /tmp/file.csv
echo -n '{ "x": 200 }' > /tmp/file.json
echo '- a' > /tmp/file.yaml
echo '- b' >> /tmp/file.yaml


bundle agent main

      "csv" data => readdata("/tmp/file.csv", "auto"); # or file type "CSV"
      "json" data => readdata("/tmp/file.json", "auto"); # or file type "JSON"

      "csv_str" string => format("%S", csv);
      "json_str" string => format("%S", json);

    feature_yaml:: # we can only test YAML data if libyaml is compiled in
      "yaml" data => readdata("/tmp/file.yaml", "auto"); # or file type "YAML"
      "yaml_str" string => format("%S", yaml);

      "From /tmp/file.csv, got data $(csv_str)";
      "From /tmp/file.json, got data $(json_str)";
      "From /tmp/file.yaml, we would get data $(yaml_str)";
    !feature_yaml:: # show the output anyway
      'From /tmp/file.yaml, we would get data ["a","b"]';



R: From /tmp/file.csv, got data [["1","2","3"]]
R: From /tmp/file.json, got data {"x":200}
R: From /tmp/file.yaml, we would get data ["a","b"]

See also: readcsv(), readyaml(), readjson(), readenvfile(), validdata(), data documentation.

History: Was introduced in 3.7.0.