Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
CoffeeScript to JSON script for OS X
coffee --print --compile $1.coffee | sed '1s/^.//' | sed 's/ });/}/' | sed "s/\([^ '\"].*[^ '\"]\):/\"\1\":/" > $1.json
@aral

This comment has been minimized.

Show comment Hide comment
@aral

aral Nov 13, 2011

Converts a CoffeeScript data structure into valid JSON (simply removes the additional parentheses that the compiler adds to the start and end of the data structure.)

Owner

aral commented Nov 13, 2011

Converts a CoffeeScript data structure into valid JSON (simply removes the additional parentheses that the compiler adds to the start and end of the data structure.)

@jbilcke

This comment has been minimized.

Show comment Hide comment
@jbilcke

jbilcke Nov 21, 2011

hmm it seems to have changed a little bit, when I run it on my node:

$ cat foo.coffee
foo: "bar"

$ ./coffee2js foo

$ cat foo.json
function() {
({
"foo": "bar"
}
}).call(this);

jbilcke commented Nov 21, 2011

hmm it seems to have changed a little bit, when I run it on my node:

$ cat foo.coffee
foo: "bar"

$ ./coffee2js foo

$ cat foo.json
function() {
({
"foo": "bar"
}
}).call(this);

@wlaurance

This comment has been minimized.

Show comment Hide comment
@wlaurance

wlaurance Feb 15, 2012

coffee --print --bare $1.coffee | sed '1s/&.//' | sed 's/});/}/' | sed 's/({/{/' | sed "s/\([^ '\"].*[^ '\"]\): /\"\1\": /1" > $1.json

Try this. Note the spaces after :
I have URLS as values and this was causing it to put quotes around part of it. The space after : fixed this.

coffee --print --bare $1.coffee | sed '1s/&.//' | sed 's/});/}/' | sed 's/({/{/' | sed "s/\([^ '\"].*[^ '\"]\): /\"\1\": /1" > $1.json

Try this. Note the spaces after :
I have URLS as values and this was causing it to put quotes around part of it. The space after : fixed this.

@onetom

This comment has been minimized.

Show comment Hide comment
@onetom

onetom Nov 28, 2013

With a bit more simplification and handling the input both from stdin and from a file name parameter:

#!/bin/sh
echo \{
    cat $1 | \
    coffee --print --bare --compile --stdio | \
    sed -e '1,2d; $d' | \
    sed "s/\([^ '\"].*[^ '\"]\): /\"\1\": /"
echo \}

onetom commented Nov 28, 2013

With a bit more simplification and handling the input both from stdin and from a file name parameter:

#!/bin/sh
echo \{
    cat $1 | \
    coffee --print --bare --compile --stdio | \
    sed -e '1,2d; $d' | \
    sed "s/\([^ '\"].*[^ '\"]\): /\"\1\": /"
echo \}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment