Convert .json to .csv in ruby
Try something like this:
require 'csv'
require 'json'
csv_string = CSV.generate do |csv|
JSON.parse(File.open("foo.json").read).each do |hash|
csv << hash.values
end
end
puts csv_string
CSV to JSON Ruby Script?
This is easy in ruby 1.9 where data is your csv data string
require 'csv'
require 'json'
CSV.parse(data).to_json
How to convert JSON to CSV format and store in a variable
Ok I finally got this code working:
<html>
<head>
<title>Demo - Covnert JSON to CSV</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>
<script type="text/javascript">
// JSON to CSV Converter
function ConvertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}
// Example
$(document).ready(function () {
// Create Object
var items = [
{ name: "Item 1", color: "Green", size: "X-Large" },
{ name: "Item 2", color: "Green", size: "X-Large" },
{ name: "Item 3", color: "Green", size: "X-Large" }];
// Convert Object to JSON
var jsonObject = JSON.stringify(items);
// Display JSON
$('#json').text(jsonObject);
// Convert JSON to CSV & Display CSV
$('#csv').text(ConvertToCSV(jsonObject));
});
</script>
</head>
<body>
<h1>
JSON</h1>
<pre id="json"></pre>
<h1>
CSV</h1>
<pre id="csv"></pre>
</body>
</html>
Thanks alot for all the support to all the contributors.
Praney
Converting CSV to JSON in bash
The right tool for this job is jq
.
jq -Rsn '
{"occurrences":
[inputs
| . / "\n"
| (.[] | select(length > 0) | . / ";") as $input
| {"position": [$input[0], $input[1]], "taxo": {"espece": $input[2]}}]}
' <se.csv
emits, given your input:
{
"occurences": [
{
"position": [
"-21.3214077",
"55.4851413"
],
"taxo": {
"espece": "Ruizia cordata"
}
},
{
"position": [
"-21.3213078",
"55.4849803"
],
"taxo": {
"espece": "Cossinia pinnata"
}
}
]
}
By the way, a less-buggy version of your original script might look like:
#!/usr/bin/env bash
items=( )
while IFS=';' read -r lat long pos _; do
printf -v item '{ "position": [%s, %s], "taxo": {"espece": "%s"}}' "$lat" "$long" "$pos"
items+=( "$item" )
done <se.csv
IFS=','
printf '{"occurrences": [%s]}\n' "${items[*]}"
Note:
- There's absolutely no point using
cat
to pipe into a loop (and good reasons not to); thus, we're using a redirection (<
) to open the file directly as the loop's stdin. read
can be passed a list of destination variables; there's thus no need to read into an array (or first to read into a string, and then to generate a heresting and to read from that into an array). The_
at the end ensures that extra columns are discarded (by putting them into the dummy variable named_
) rather than appended topos
."${array[*]}"
generates a string by concatenating elements ofarray
with the character inIFS
; we can thus use this to ensure that commas are present in the output only when they're needed.printf
is used in preference toecho
, as advised in the APPLICATION USAGE section of the specification forecho
itself.- This is still inherently buggy since it's generating JSON via string concatenation. Don't use it.
Convert CSV to JSON while dropping certain columns
This is easily done with pandas:
import pandas as pd
df = pd.read_csv('data.csv')
df[['LOCATION', 'TIME', 'Value']].to_json(orient='records')
the orient='records'
part is important, otherwise it will be grouped by columns instead of rows
How to convert CSV file to multiline JSON?
The problem with your desired output is that it is not valid json document,; it's a stream of json documents!
That's okay, if its what you need, but that means that for each document you want in your output, you'll have to call json.dumps
.
Since the newline you want separating your documents is not contained in those documents, you're on the hook for supplying it yourself. So we just need to pull the loop out of the call to json.dump and interpose newlines for each document written.
import csv
import json
csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')
fieldnames = ("FirstName","LastName","IDNumber","Message")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('\n')
Related Topics
Nicely Formatting Output to Console, Specifying Number of Tabs
How to Create a Delete Link for a Related Object in Ruby on Rails
Is There a Way in Ruby/Rails to Execute Code That Is in a String
Puppet/Facter "Could Not Retrieve Fact Fqdn": How to Fix or Circumvent
In Ruby, What Structures Can a 'Rescue' Statement Be Nested In
Setting Request Headers in Ruby
Difference Between Map and Each
Rails: Modifying a Model Generated by Scaffolding
Ruby 1.9, Force_Encoding, But Check
Rails - Invalid Authenticity Token After Deploy
Rails 4 Before_Action, Pass Parameters to Invoked Method
Concatenating String with Number in Ruby
How to Access Url Helper from Rails Module
How to Describe an Enumeration Column in a Rails 3 Migration
How to Get the Parent's Class Name in Ruby
Getting Rails Url Helpers to Automatically Output Https Urls