Top Level Namespace

Defined Under Namespace

Classes: Mysql2json

Instance Method Summary collapse

Instance Method Details

#convert(results) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mysql2json.rb', line 11

def convert(results)
	non_json_data = []

	results = Array(results)

	results.each_index do |index|
		non_json_data.push(results[index]);
		index += 1
		puts 'row: '+index.to_s+' parsed'
	end

	non_json_data.each do |k,v|
		v.to_s
	end

	json_data = non_json_data.map { |o| Hash[o.each_pair.to_a] }.to_json

	if is_json_valid(json_data)
		json_data = json_data.to_s
	else
		json_data = 'ParserError: Could not parse to json, retry.'
	end

	return json_data
end

#is_json_valid(object) ⇒ Object



4
5
6
7
8
9
# File 'lib/mysql2json.rb', line 4

def is_json_valid(object)
	JSON.parse(object)
	return true
rescue JSON::ParserError
	return false
end