Skip to content

Instantly share code, notes, and snippets.

@dmmfll
Created February 23, 2017 17:10
Show Gist options
  • Save dmmfll/126b4ec98557bc0bd267641b6edbed96 to your computer and use it in GitHub Desktop.
Save dmmfll/126b4ec98557bc0bd267641b6edbed96 to your computer and use it in GitHub Desktop.
gist_url
auto_gistup
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# require the builtin gem\n",
"\n",
"require 'json'"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[{\"yzpszuihiogiozhomoaa\"=>0}, {\"adxzrimwmsvxbqmqmijs\"=>1}, {\"jtdtopbmyiipueswtxgu\"=>2}, {\"rvsdfgygvredhocksdfe\"=>3}, {\"hnonsqfxxexezzfhdzwd\"=>4}, {\"drdpyxnqmqmvkxposusg\"=>5}, {\"mxpztndokhkccyumxact\"=>6}, {\"hkfmuvzscatklubgqjlv\"=>7}, {\"dxbykzidyqhofbjqliun\"=>8}, {\"eynvxsrfkmyasdixaddg\"=>9}]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create some fake data: an array of hashes\n",
"\n",
"data = 10.times.each_with_index.map do |i|\n",
" {20.times.map do\n",
" (\"a\"..\"z\").to_a.sample\n",
" end.join => i}\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\"[{\\\"yzpszuihiogiozhomoaa\\\":0},{\\\"adxzrimwmsvxbqmqmijs\\\":1},{\\\"jtdtopbmyiipueswtxgu\\\":2},{\\\"rvsdfgygvredhocksdfe\\\":3},{\\\"hnonsqfxxexezzfhdzwd\\\":4},{\\\"drdpyxnqmqmvkxposusg\\\":5},{\\\"mxpztndokhkccyumxact\\\":6},{\\\"hkfmuvzscatklubgqjlv\\\":7},{\\\"dxbykzidyqhofbjqliun\\\":8},{\\\"eynvxsrfkmyasdixaddg\\\":9}]\""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# turn data into json string which is the same thing a lot of API's will return\n",
"# in an HTTP response body\n",
"\n",
"json_data = data.to_json"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"String\n"
]
},
{
"data": {
"text/plain": [
"String"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p json_data.class"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{\"yzpszuihiogiozhomoaa\"=>0}, {\"adxzrimwmsvxbqmqmijs\"=>1}, {\"jtdtopbmyiipueswtxgu\"=>2}, {\"rvsdfgygvredhocksdfe\"=>3}, {\"hnonsqfxxexezzfhdzwd\"=>4}, {\"drdpyxnqmqmvkxposusg\"=>5}, {\"mxpztndokhkccyumxact\"=>6}, {\"hkfmuvzscatklubgqjlv\"=>7}, {\"dxbykzidyqhofbjqliun\"=>8}, {\"eynvxsrfkmyasdixaddg\"=>9}]\n",
"Array\n"
]
},
{
"data": {
"text/plain": [
"Array"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# undo the process\n",
"# this is what you do with the JSON string returned in\n",
"# the body of an HTTP response\n",
"\n",
"converted_data = JSON.parse(json_data)\n",
"p converted_data\n",
"p converted_data.class"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 2.3.0",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": ".rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "2.3.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
# require the builtin gem
require 'json'
# create some fake data: an array of hashes
data = 10.times.each_with_index.map do |i|
{20.times.map do
("a".."z").to_a.sample
end.join => i}
end
# turn data into json string which is the same thing a lot of API's will return
# in an HTTP response body
json_data = data.to_json
p json_data.class
# undo the process
# this is what you do with the JSON string returned in
# the body of an HTTP response
converted_data = JSON.parse(json_data)
p converted_data
p converted_data.class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment