Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON to dataframe input and output #873

Closed
mihirparadkar opened this issue Oct 3, 2015 · 3 comments
Closed

JSON to dataframe input and output #873

mihirparadkar opened this issue Oct 3, 2015 · 3 comments

Comments

@mihirparadkar
Copy link

Could dataframes be read from and output to JSON? I know that pandas and R do it and it's a very universal format. Here are some simple implementations of readjson and writejson using the JSON.jl package.
readjson:

using JSON
using Lazy
using DataFrames

function readjson(filename::String)
  rawdata = JSON.parsefile(filename)
  names = @as file rawdata begin
                map(keys,file)
                vcat([[i...] for i in file]...)
                unique(file)
          end
  df = DataFrame()
  for name in names
    df[symbol(name)] = map(rawdata) do obj
      haskey(obj,name) ? obj[name] : NA
    end
    for i in 1:length(df[symbol(name)])
      if df[symbol(name)][i] == nothing
        df[symbol(name)][i] = NA
      end
    end
  end
  return df
end

and writejson:

using JSON
using DataFrames

function df2json(df::DataFrame)
  len = length(df[:,1])
  indices = names(df)
  jsonarray = [Dict([string(index)=> (isna(df[index][i])? nothing : df[index][i])
                                                    for index in indices])
                                                          for i in 1:len]
  return JSON.json(jsonarray)
end

function writejson(path::String,df::DataFrame)
  f = open(path,"w")
  write(f,df2json(df))
  close(f)
end
@mihirparadkar
Copy link
Author

The readjson code works for data of the format

[
 {"name1":value11 , "name2":value21 ...},
 {"name1":value12 , "name2":value22 ...},
 ...
]

@rawls238
Copy link

@mihirparadkar why not put up a PR for this?

@bkamins bkamins mentioned this issue Jan 15, 2019
31 tasks
@bkamins
Copy link
Member

bkamins commented Jul 25, 2019

Closing as this is handled by JSONTables.jl now thanks to @quinnj.

@bkamins bkamins closed this as completed Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants