Error when trying to open a file

I am writing what I thought would be a straight-forward piece of julia code to fix an issue with a json file in multiple folders.

using JSON

root = "C:\\ROOTFOLDER\\"
cd(root)
filename = "my.json"

folderList = ["A", "B", "C"]

function fixJSON(root, folderList, filename)
    for folder in folderList
        path = root * folder
        cd(path)
        jsonData = JSON.parsefile(filename)
        jsonData["a"] = "Hello"
        open(filename, "w") do f
            JSON.print(f, simConfig, 4) # Pretty print!
        end
        cd(root)
    end
end

fixJSON(root, folderList, filename)

However, I get the following error when I run the script:

ERROR: LoadError: SystemError: opening file "my.json": Invalid argument
Stacktrace:
  [1] systemerror(p::String, errno::Int32; extrainfo::Nothing)
    @ Base .\error.jl:174
  [2] #systemerror#68
    @ .\error.jl:173 [inlined]
  [3] systemerror
    @ .\error.jl:173 [inlined]
  [4] open(fname::String; lock::Bool, read::Nothing, write::Nothing, create::Nothing, truncate::Bool, append::Nothing)
    @ Base .\iostream.jl:293
  [5] open(fname::String, mode::String; lock::Bool)
    @ Base .\iostream.jl:355
  [6] open(fname::String, mode::String)
    @ Base .\iostream.jl:355
  [7] open(::var"#1#2"{Dict{String, Any}}, ::String, ::Vararg{String}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base .\io.jl:328
  [8] open(::Function, ::String, ::String)
    @ Base .\io.jl:328
  [9] fixJSON(root::String, folderList::Vector{String}, filename::String)
    @ Main C:\ROOTFOLDER\updateJSON.jl:16
 [10] top-level scope
    @ C:\PCAR\updateJSON.jl:23
 [11] include(fname::String)
    @ Base.MainInclude .\client.jl:451
 [12] top-level scope
    @ REPL[3]:1
in expression starting at C:\ROOTFOLDER\updateJSON.jl:23

I probably am missing something simple. Why\How is JSON.parsefile(filename) able to read the file and get the data but open(filename, "w") report an error?

Judging from a similar issue Invalid argument error when saving a loaded dataframe · Issue #170 · JuliaData/CSV.jl · GitHub this might be a Windows issue with memory mapped files.

Doing JSON.parsefile(...; use_mmap=false) should help.

By the way, instead of cd’ing around, I would construct the filename with joinpath(root, folder, filename).

3 Likes

@skleinbo use_mmap=false did the trick.

1 Like