Export DataFrame to JSON in Julia

Export DataFrame to JSON in Julia

  • Julia
  • 2 mins read

To export a dataframe to JSON in Julia, you can use the JSON.json() function from the JSON package. Here is an example of how you can use this function to save a dataframe to a JSON file:

Export DataFrame to JSON Example in Julia

using JSON

# define the dataframe
df = DataFrame(A = [1, 2, 3], B = [4, 5, 6])

# convert the dataframe to a JSON string
json_str = JSON.json(df)

# write the JSON string to a file
open("df.json", "w") do io
    write(io, json_str)
end

This will save the dataframe to a file named "df.json" in the current working directory. The data will be saved as a JSON string, with each row of the dataframe represented as an object in the JSON array.

Output:

{"A":[1,2,3],"B":[4,5,6]}

If you want to specify a different location for the JSON file, you can provide the full file path when opening the file for writing. For example:

open("/path/to/df.json", "w") do io
    write(io, json_str)
end

You can also customize the way the dataframe is converted to a JSON string by specifying additional options to the JSON.json() function. For example, you can specify the indentation level, the date format, and whether or not to convert missing values to null. For more information and a complete list of options, see the documentation for the JSON.json() function.

Related: