# Json variable passing

**URL:** <https://rubytalk.org/t/json-variable-passing/68734>\
**Category:** ruby-talk\
**Created:** [12 November 2013 18:45 UTC](https://rubytalk.org/t/json-variable-passing/68734 "2013-11-12T18:45:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [12 November 2013 18:45 UTC](https://rubytalk.org/t/json-variable-passing/68734/1 "2013-11-12T18:45:05Z")

</div>

hello all (ruby newbie) -

i have been using PHP for some time now to take a big complicated data  
structure and pass it right into jScript using something like the  
following:

const Foo=JSON.parse('\<?php echo addslashes(json\_encode($Foo)); ?\>');  
--or a shorter version--  
const Foo=JSON.parse('\<?=addslashes(json\_encode($Foo))?\>');

for the non-php types, all this is really doing is to take any  
structure, make it json friendly, and then include it as a jScript  
constant variable.

so, i can run some big mysql statement, put the results into a php  
array-variable, and make the results available to a jScript constant.

is there a simple way in ruby to do the same thing?

i am looking for a one-liner to move massive amounts of data.

from what little i have seen with ruby, there seems to be virtually  
nothing it cant do better than php.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple1](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Tamara\_Temple1](https://rubytalk.org/u/Tamara_Temple1)\
**Post date:** [12 November 2013 23:19 UTC](https://rubytalk.org/t/json-variable-passing/68734/2 "2013-11-12T23:19:54Z")

</div>

Oh yes.

Let’s assume a similar setup, where you want to fill in a javascript data structure with JSON supplied on the server before you send it down to the client.

Using the standard ERB stuff:

const Foo = \<%= big\_extraction\_of\_data.to\_json %\>

the big\_extraction\_of\_data could be something that returns or contains an Array or Hash object, or possibly an ActiveRecord Relation or any other type of class that implements the .to\_json message.

I’m not sure you need the JSON.parse('…stringified json…') there even in the php case, do you? it should just basically imprint json directly into the page at that point.

> **···**
>
> On Nov 12, 2013, at 12:45 PM, mark edwards \<lists@ruby-forum.com\> wrote:
> 
> > hello all (ruby newbie) -
> > 
> > i have been using PHP for some time now to take a big complicated data  
> > structure and pass it right into jScript using something like the  
> > following:
> > 
> > const Foo=JSON.parse('\<?php echo addslashes(json\_encode($Foo)); ?\>');  
> > --or a shorter version--  
> > const Foo=JSON.parse('\<?=addslashes(json\_encode($Foo))?\>');
> > 
> > for the non-php types, all this is really doing is to take any  
> > structure, make it json friendly, and then include it as a jScript  
> > constant variable.
> > 
> > so, i can run some big mysql statement, put the results into a php  
> > array-variable, and make the results available to a jScript constant.
> > 
> > is there a simple way in ruby to do the same thing?
> > 
> > i am looking for a one-liner to move massive amounts of data.
> > 
> > from what little i have seen with ruby, there seems to be virtually  
> > nothing it cant do better than php.
> > 
> > —

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [13 November 2013 01:03 UTC](https://rubytalk.org/t/json-variable-passing/68734/3 "2013-11-13T01:03:52Z")

</div>

tamouse - thank you very much for taking the time to answer my question.  
it looks even simpler and cleaner in ruby !

we needed the jScript command "JSON.parse" because of quotes being  
passed and other characters inside the json-string that might interfere  
with the string loading properly.

i am thinking something like this might work:

const Foo = JSON.parse("\<%= big\_extraction\_of\_data.to\_json %\>");

side-note: i find it extremely useful to create a monster structure and  
just pass it down to jScript as is, and let jScript (or jQuery) do all  
the work. i have been able to collapse the size of pages this way quite  
dramatically, assuming my user will probably never need to see all the  
data at the same time.

i have my users click on a link, then the html is auto-generated via  
jQuery and that code is used in a pop-up window. the overall page size  
is probably only 10% of what it takes when all the html is generated  
from the server.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [13 November 2013 16:26 UTC](https://rubytalk.org/t/json-variable-passing/68734/4 "2013-11-13T16:26:29Z")

</div>

tamouse - once again, thank you for your time and your patience with  
newbies!

where i ran into trouble was passing a string like this:

This ruby string contains double "quote" characters and single 'quote'  
characters

using php, jScript was getting very confused with the quoting. its like  
all double-quotes needed to be back-slashed, changed to %22, or  
something like that. but i will give this a try in ruby using your  
approach.

assuming this works, you will put a huge smile on my face!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple1](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Tamara\_Temple1](https://rubytalk.org/u/Tamara_Temple1)\
**Post date:** [13 November 2013 04:10 UTC](https://rubytalk.org/t/json-variable-passing/68734/5 "2013-11-13T04:10:53Z")

</div>

> tamouse - thank you very much for taking the time to answer my question.  
> it looks even simpler and cleaner in ruby !
> 
> we needed the jScript command "JSON.parse" because of quotes being  
> passed and other characters inside the json-string that might interfere  
> with the string loading properly.

But in this case, the JSON object data is right in the code, no need to quote it, then parse it client side if you just write it directly into the source you’re sending.

Here’s a tiny example, based on sinatra:

&nbsp;&nbsp;get '/testjson' do ||  
&nbsp;&nbsp;&nbsp;&nbsp;erb :testjson, locals: {this\_hash: {a: [1,2,3], "BBB" =\> "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enimad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "classic" =\> ["a fine thing", "I do now", "blueberry", :pudding, 3.1415]}}  
&nbsp;&nbsp;end

Then the erb view testjson.erb contains:

\<h1\>Testing sending JSON directly\</h1\>

\<script\>  
const Foo = \<%= this\_hash.to\_json %\>;  
console.log(Foo);  
\</script\>

and this is the source html in the browser:

\<h1\>Testing sending JSON directly\</h1\>

\<script\>  
const Foo = {"a":[1,2,3],"BBB":"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enimad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.","classic":["a fine thing","I do now","blueberry","pudding",3.1415]};  
console.log(Foo);  
\</script\>

So, no extra quoting, parsing, etc.

> i am thinking something like this might work:
> 
> const Foo = JSON.parse("\<%= big\_extraction\_of\_data.to\_json %\>");
> 
> side-note: i find it extremely useful to create a monster structure and  
> just pass it down to jScript as is, and let jScript (or jQuery) do all  
> the work. i have been able to collapse the size of pages this way quite  
> dramatically, assuming my user will probably never need to see all the  
> data at the same time.

Putting it in a string, then parsing it just seems like extra work. Just insert the JSON object in directly, and assign it.

> i have my users click on a link, then the html is auto-generated via  
> jQuery and that code is used in a pop-up window. the overall page size  
> is probably only 10% of what it takes when all the html is generated  
> from the server.

It’s certainly valid for a lot of interactions.

> **···**
>
> On Nov 12, 2013, at 7:03 PM, mark edwards \<lists@ruby-forum.com\> wrote:
> 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple1](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Tamara\_Temple1](https://rubytalk.org/u/Tamara_Temple1)\
**Post date:** [14 November 2013 04:15 UTC](https://rubytalk.org/t/json-variable-passing/68734/6 "2013-11-14T04:15:49Z")

</div>

I think even in your PHP case, you were unnecessarily putting the JSON in a string and parsing it on the client side.

To keep the ruby list pure, I’ve shuffled off a PHP version to this gist: [Passing a Big Complicated Data Structure in PHP directly into the client context in Javascript. (no magic) · GitHub](https://gist.github.com/tamouse/7461180)

> **···**
>
> On Nov 13, 2013, at 10:26 AM, mark edwards \<lists@ruby-forum.com\> wrote:
> 
> > tamouse - once again, thank you for your time and your patience with  
> > newbies!
> > 
> > where i ran into trouble was passing a string like this:
> > 
> > This ruby string contains double "quote" characters and single 'quote'  
> > characters
> > 
> > using php, jScript was getting very confused with the quoting. its like  
> > all double-quotes needed to be back-slashed, changed to %22, or  
> > something like that. but i will give this a try in ruby using your  
> > approach.
> > 
> > assuming this works, you will put a huge smile on my face!
