74
\$\begingroup\$

This challenge is straightforward, but hopefully, there are plenty of avenues you can approach it:

You need to print/return a valid JSON object of at least 15 characters, not counting unessential whitespace. Your program should work without any input.

In the interest of clarity, a JSON object starts and ends with curly braces {}, and contains zero or more key:value pairs separated by commas. The full JSON specification can be found at json.org, and the output of your code must pass this validator.

Therefore, any of the following would not be valid:

4                               //Too short, not an object
"really, really long string"    //A string, not an object
["an","array","of","values"]    //An array is not a JSON object
{"this":4      }                //You can't count unessential whitespace
{"1":1,"2":3}                   //Too short
{"a really long string"}        //Not valid JSON, it needs a value
{'single-quoted':3}             //JSON requires double-quotes for strings

However, the following would be valid:

{"1":1,"2":2,"3":3,"4":4}       //Long enough
{"whitespace      ":4}          //This whitespace isn't unessential

Non-programming languages are allowed on this challenge. You may return a string from a function, or print it out. This is a , so answer it with as little code as possible!

\$\endgroup\$
  • 1
    \$\begingroup\$ I like the variety of answers on this one \$\endgroup\$ – Robert Fraser Oct 25 '16 at 6:59
  • \$\begingroup\$ Hmmmm, your definition of JSON is limited. What about code that ouputs valid JSON but does not output curly braces? \$\endgroup\$ – Konijn Oct 25 '16 at 13:01
  • 4
    \$\begingroup\$ @Konijn like I said, it must be a valid JSON object. The object is defined by the curly braces. \$\endgroup\$ – Nathan Merrill Oct 25 '16 at 14:01
  • \$\begingroup\$ Got it, with stress on object ;) \$\endgroup\$ – Konijn Oct 25 '16 at 14:06
  • 1
    \$\begingroup\$ @Masterzagh Unfortunately, a native JS object doesn't count. "You may return a string from a function, or print it out" \$\endgroup\$ – Nathan Merrill Feb 2 '17 at 15:05

70 Answers 70

3
\$\begingroup\$

Bash, 18 bytes

echo {\"$PATH\":1}

This works on Mac and Linux, unless your $PATH isn't set.

\$\endgroup\$
  • \$\begingroup\$ This is genius. +1. And welcome to the site! \$\endgroup\$ – Rɪᴋᴇʀ Oct 29 '16 at 0:23
3
\$\begingroup\$

Japt -Q, 1 byte

M

Try it online!

The output is {"P":3.141592653589793,"Q":1.618033988749895,"T":6.283185307179586}.

How it works

M is a shorthand for JS's Math object, with one-char aliases to existing functions/constants and some extra functions/constants. The constants shown here are:

  • M.P = Math.PI
  • M.Q = Golden Ratio = (Math.sqrt(5)+1)/2
  • M.T = Math.PI * 2

And the boring part: the -Q flag applies JSON.stringify to the output value. Some of the properties are lost (e.g. Math.E and Math.SQRT2), but we still have the above three values and the result is well over 15 characters.

\$\endgroup\$
2
\$\begingroup\$

Jelly, 13 12 bytes

“{"":}”s2jȷ4

TryItOnline!

How?

“{"":}”s2jȷ4 - Main link: no arguments
“{"":}”      - literal ['{','"','"',':','}']
       s2    - split into twos -> [['{','"'],['"',':'],['}']]
         j   - join with
          ȷ4 - literal 10000 -> ['{','"',10000,'"',':',10000,'}']
             - implicit print -> {"10000":10000}

previous (13):

“"11”m0“{:}”j
\$\endgroup\$
2
\$\begingroup\$

IBM/Lotus Notes @Formula, 24 23 bytes

Computed value in a Notes field.

"{\"\":"+@Text(@Pi)+"}"

Outputs:

{"":3.14159265358979}

Nothing clever - just uses the shortest possible function that outputs a number then converts it to text for display.

\$\endgroup\$
  • 1
    \$\begingroup\$ Useless? Why? You can still refer that value by its key and use it: pastebin.com/RqbbUBEt \$\endgroup\$ – manatwork Oct 25 '16 at 10:33
  • \$\begingroup\$ I guess so. If it is considered acceptable then I'll edit my answer accordingly. \$\endgroup\$ – ElPedro Oct 25 '16 at 10:35
2
\$\begingroup\$

Japt, 11 bytes

There are a multitude of 11-byte programs:

`\{"{O}":1}  // {"[object Object]":1}
`\{"{M}":1}  // {"[object Math]":1}
`\{"{@}":1}  // {"function (X,Y,Z){return }":1}
`\{"{_}":1}  // {"function (Z){return Z}":1}
`\{"{Ð}":1}  // {"Tue Oct 25 2016 12:15:24 GMT-0400 (Eastern Standard Time)":1}
`\{"":{+Ð}}  // {"":1477412000095}

The last two use the Date object; thus, they are non-constant, but always output more than 15 bytes. Explanation:

`\{"   ":1}` // Take this string, 
    {O}      // inserting variable O here.
             // O is an Object, which stringifies to "[object Object]".
             // Implicitly output.

Test it online!

More interesting 12-byte version:

`\{":{L²}}`²

Prints {":10000}{":10000}. Explanation:

`\{":    }`   // Take this string, 
     {L²}     // inserting 100 squared (10000) here,
              // (this yields {":10000})
           ²  // and double.
              // Implicitly output.

Test it online!

\$\endgroup\$
2
\$\begingroup\$

Common Lisp, 24 bytes

(format t"{~S:~f}""p"pi)

Prints the following to standard output (23 characters long):

{"p":3.141592653589793}

This is a simple string format which uses the predefined constant PI.

\$\endgroup\$
2
\$\begingroup\$

q, 14 bytes

Prints memory usage information about the current process

-1@.j.j .Q.w`;

Sample output:

q)-1@.j.j .Q.w`;
{"used":127392,"heap":67108864,"peak":67108864,"wmax":0,"mmap":0,"mphy":16735457280,"syms":585,"symw":18925}
\$\endgroup\$
  • 2
    \$\begingroup\$ Could you provide sample output? \$\endgroup\$ – Nathan Merrill Oct 25 '16 at 14:09
  • \$\begingroup\$ sure, updated post \$\endgroup\$ – skeevey Oct 25 '16 at 17:48
2
\$\begingroup\$

CoffeeScript, 18 16 bytes

Saved 2 bytes thanks to @Caffeinated.tech

->'{"":'+1/3+'}'

Outputs {"":0.3333333333333333}. Sadly, because of the required double-quotes, string interpolation is a byte longer:

->"{\"\":#{1/3}}"
\$\endgroup\$
  • \$\begingroup\$ You can use the fact that float division is default in Coffee, which let's us use 1/3 instead of undefined to give the length of the required output. I got it to 16 chars: ->'{"":'+1/3+'}' \$\endgroup\$ – caffeinated.tech Oct 26 '16 at 12:36
  • 1
    \$\begingroup\$ @Caffeinated.tech The main reason I didn't think of that was that I didn't realize that Coffee's functions could be arg-less, so thanks for teaching me that :) \$\endgroup\$ – ETHproductions Oct 26 '16 at 13:18
2
\$\begingroup\$

Charcoal, 7 bytes

{"⁹":9}

Try it online!

Runs of printable ASCII characters in Charcoal form literal strings, and expressions lacking any explicit command are printed. So {" and ":9} are just output verbatim. , however, is an integer literal, and when you print an integer, you get an ASCII-art line that many characters long. The character used for horizonal lines is the hyphen, so the result is:

{"---------":9}

Conveniently, this is exactly 15 characters.

\$\endgroup\$
2
\$\begingroup\$

TSQL(2016 only), 50 BYTES

SELECT*FROM(VALUES(GETDATE()))AS A(a)FOR JSON PATH
\$\endgroup\$
  • \$\begingroup\$ You don't require the AS for the alias and NEWID instead of GETDATE will save a couple as well. \$\endgroup\$ – MickyT Jun 20 '17 at 1:36
2
\$\begingroup\$

Java 8 (Full Program), 125 Bytes 123 Bytes 116 Bytes

Yes, I know there are shorter ways in Java, as posted above. I just wanted to approach it from a looping perspective.

Thanks to ais523 for saving 2 bytes.
Thanks to Manatwork for saving 7 bytes.

interface C{static void main(String[]a){for(int i=0;i++<9;)System.out.print(i<2?"{":"\""+i+"\":"+i+(i>8?"}":","));}}

Ungolfed

interface C{
    static void main(String[]a){
        //Loop from 0-8, add one to i at start of loop instead of end though
        for(int i=0;i++<9;)
            //If it is a start case simply print { otherwise print "i":i
            //If it is an end case also print } otherwise also print :
            System.out.print(i<2?"{":'"'+i+"\":"+i+(i>8?"}":","));
        }
}
\$\endgroup\$
  • \$\begingroup\$ Assuming you want to keep with this algorithm (which is almost certainly not the best for optimizing your score), you can nonetheless make the program a bit shorter by using < and > comparisons rather than ==. \$\endgroup\$ – user62131 Jun 19 '17 at 10:19
  • \$\begingroup\$ Good idea, I'll update the code. I don't know why but I really wanted to make a loop. \$\endgroup\$ – Llew Vallis Jun 19 '17 at 10:20
  • \$\begingroup\$ You have an accidental ` left at the end of your golfed code. While removing it, you could also remove the unnecessary {} around for's only statement and change "\""'"'. \$\endgroup\$ – manatwork Jun 19 '17 at 10:26
  • \$\begingroup\$ There would be one more way to reduce it: i<2?"{":"\""+i+"\":"+i+(i>8?"}":","). With this the "1":1, part vanishes from the output, though it remains long enough to be valid. \$\endgroup\$ – manatwork Jun 19 '17 at 10:35
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Martin Ender Jun 19 '17 at 10:36
1
\$\begingroup\$

Turtlèd, 15 bytes

'{r'"10:'"r":3}

Try it online

outputs {" ":3}

Explanation

'{               write {
  r              move right, off {
   '"            write "
     10          put 10 in register
       :         move right that many characters (puts spaces in)
        '"r       write ", move right of that character
           ":3}   write :3}
\$\endgroup\$
1
\$\begingroup\$

Myth, 28 26 bytes

{"a":"{\":3141}","_":"aa"}

Saved 2 bytes by adapting the solution given by the /// answer.

Myth is a language similar to Thue, contained in a valid JSON object. How appropriate! Here, _ is the initial state, and replaces a with "****". Output:

{":3141}{":3141}

For clarity, I added some spaces:

{
  ":3141}{"  :  3141
}

Previous solutions

28 bytes: {"a":"\"****\"","_":"{a:a}"}. Outputs: {"****":"****"}

Running

I found the interpreter here:

function(m,y,t,h){m=JSON.parse(m);for(y=m._;h=1;){for(t in m)if(t!="_"&&~y.indexOf(t)){y=y.replace(t,m[t]);h=0;break}if(h)break}return y}

Test it out right here:

function myth(m,y,t,h){m=JSON.parse(m);for(y=m._;h=1;){for(t in m)if(t!="_"&&~y.indexOf(t)){y=y.replace(t,m[t]);h=0;break}if(h)break}return y}
b.onclick=function(){o.innerHTML="";o.appendChild(document.createTextNode(myth(q.value)));}
textarea {
  width: 90%;
}
*{font-family:Consolas,monospace;}
<textarea id=q>{"a":"\"****\"","_":"{a:a}"}</textarea>
<br>
<button id=b>execute myth code</button>
<br>
<textarea id=o disabled>output</textarea>

\$\endgroup\$
1
\$\begingroup\$

Addict, 200 167 bytes

Addict is a Turing tarpit where the only control flow is user-defined commands.

a A
 i 1
 i 1
 d
a B
 A 1
 A 1
 d
a C
 B 1
 B 1
 d
a D
 C 1
 C 1
 d
D b
D b
D b
D b
D b
D b
D b
C b
B b
d b
D q
D q
A q
c b
c q
c q
D q
C q
c q
n b
n q
n b
n q
A b
c b

This outputs {"":1235812358}. Try it in the online interpreter!

How it works

To golf answer space, I'm going to refer you to the sections Primer on Addict and Act I on "Hello, World!" in Addict. If you already know how that works, feel free to skip this part.

Act II

The rest of the program is devoted to outputting a valid JSON object in as few bytes as possible. In order to minimize the code, we can use the variables that are already set to output a number with the n command. The first step is then to set three variables to the two chars we need at first:

  • b to 123, the char code of {
  • q to 34, the char code of "

After these have been set, we output {"" one char at a time, then increment q by 24, resulting in 58, the charcode of :. We output that, then output the values of b and q twice, printing 1235812358. The last step is to increment b by 2 to change it to }, and then output it to finish.

If you can find any way to golf this program, please let me know!

\$\endgroup\$
1
\$\begingroup\$

Crystal, 23 bytes

puts %Q({"#{"a"*9}":0})

You can also avoid using %Q (and keep the same character count):

puts "{\"#{"a"*9}\":0}"

The above return:

{"aaaaaaaaa":0}

Which has exactly 15 characters.

\$\endgroup\$
1
\$\begingroup\$

Actually, 10 bytes

'}Qè;':@'{

Try it online!

Explanation:

'}Qè;':@'{
'}          push "}"
  Q         push this program's source code
   è        call Python's repr function (essentially just wraps the string in double-quotes)
    ;       duplicate
     ':@    push ":", swap with one copy of double-quoted source code
        '{  push "{"
            implicitly print each stack item, separated by newlines, starting with the top

Output:

{
"'}Qè;':@'{"
:
"'}Qè;':@'{"
}
\$\endgroup\$
1
\$\begingroup\$

Javascript, 14 bytes

'{"":'+1e9+'}'

Gives:

{"":1000000000}

I know that this doesn't actually print out anything, it merely returns. But so do all other Javascript solutions here.

\$\endgroup\$
  • 2
    \$\begingroup\$ You need to either define a function, or a full program (with printing). \$\endgroup\$ – Nathan Merrill Oct 26 '16 at 16:40
  • \$\begingroup\$ @NathanMerrill allmost all other javascript/ecmascript solutions do no printing. The rule about function or program was added later. \$\endgroup\$ – nl-x Oct 31 '16 at 8:46
  • \$\begingroup\$ I guess it's a REPL? \$\endgroup\$ – l4m2 Apr 20 '18 at 12:46
1
\$\begingroup\$

ECMAScript 2015, 16 bytes

_=>`{"":${9e9}}`

It will run in most modern ECMAScript environments (eg. Firefox, Chrome, MS Edge, Opera, Node…).

\$\endgroup\$
1
\$\begingroup\$

JavaScript, 17 bytes

_=>'{"'+{}+'":0}'

outputs

{"[object Object]":0}

passes validator at jslint

This works because {} will implicitly be cast to string with the + concatenation, and calling toString on an object returns "[object Object]".

\$\endgroup\$
1
\$\begingroup\$

Actionscript 3, 27 bytes

trace(JSON.stringify(int));

outputs

{"MAX_VALUE":2147483647,"length":1,"MIN_VALUE":-2147483648,"prototype":{}}

\$\endgroup\$
1
\$\begingroup\$

C#, 39 bytes

string j(){return"{\""+1.0/0+"\":42}";}

In C#, division of 0 with a double (hence the inclusion of .0), will result in Infinity.

Output:

{"Infinity":42}

\$\endgroup\$
  • \$\begingroup\$ Cool. Do you need the 0 in 1.0, or will 1./0 suffice? \$\endgroup\$ – ETHproductions Oct 28 '16 at 23:34
  • \$\begingroup\$ 1./0 is a syntax error in c#, namely "Identifier expected". Nice thought though :) \$\endgroup\$ – Travis J Oct 28 '16 at 23:55
1
\$\begingroup\$

Python 2, 13 bytes

print{"'":.1}

Outputs:

{"'": 0.10000000000000001}

Since tenths can't be stored exactly in binary Python outputs a 1 followed by 15 zeros followed by a 1 after the decimal point. Need double quotes around the single quote to not have the double quotes converted to single quotes (thanks to @JonathanAllan's comment to @xnor's answer).

\$\endgroup\$
  • \$\begingroup\$ Doesn't seem to work the same way here: pastebin.com/P5vau3WB \$\endgroup\$ – manatwork Oct 31 '16 at 8:59
  • \$\begingroup\$ @manatwork Works with QPython on my android \$\endgroup\$ – Noodle9 Oct 31 '16 at 10:52
  • \$\begingroup\$ @manatwork And Python 2.6.6 but not Python 2.7.1 \$\endgroup\$ – Noodle9 Oct 31 '16 at 13:03
1
\$\begingroup\$

Rust, 21 bytes

||"{\"a\":0.1234567}"

String substitution would be too verbose, but at least there's always terse lambdas with implicit returns.

With "test code":

fn main(){
    let f=||"{\"a\":0.1234567}";
    println!("{}", f());
}
\$\endgroup\$
1
\$\begingroup\$

Pushy, 12 bytes

`{"`A`":1}`"

Try it online!

This produces the output:

{"ABCDEFGHIJKLMNOPQRSTUVWXYZ":1}

Any text wrapped in backticks is a string literal. The A injects the whole uppercase alphabet. Then, the final " command prints it all.

\$\endgroup\$
1
\$\begingroup\$

SmileBASIC, 27 bytes

?"{
?CHR$(34)*2;":
?PI();"}

Printing quotation marks is a huge pain in BASIC. 8 bytes wasted!

Output:

{
"":
3.14159265}

Exactly 15 characters, without the whitespace.

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 15 13 bytes

„{"A„":Jû¨'}J

Try it online!

{"abcdefghijklmnopqrstuvwxyz":"zyxwvutsrqponmlkjihgfedcba"}

\$\endgroup\$
  • 2
    \$\begingroup\$ Your code outputs single quotes instead of double \$\endgroup\$ – user2428118 Feb 2 '17 at 14:25
  • \$\begingroup\$ @user2428118 fixed at a -2byte cost. JSON with ' is parsable in some languages though. \$\endgroup\$ – Magic Octopus Urn Feb 2 '17 at 14:57
1
\$\begingroup\$

PowerShell, 17 bytes

Slightly unclear on how strict the 'no input' thing is, if it's just no user input or of system input is also disallowed. While my best answer isn't shorter than the other PowerShell answer I think it is novel.

ps|ConvertTo-Json

Gets running processes (default return is an object) and converts that object to a json. Several other two and three letter aliases commands work.

Sample output omitted because it's 34115 lines long and trying to redact my username and computername was a pain.

gl|ConvertTo-Json

Gets your current filesystem location (as an object) and converts that object to a json

gci|ConvertTo-Json

Gets the files in your current path (as an object) and converts that object to a json

You can also take things like inherent type definitions (which are part of the language itself) and pipe them into the conversion function.

[xml]|ConvertTo-Json

Gets the definition of the xml datatype and converts that object to a json

[int]|ConvertTo-Json

Gets the definition of the 32bit integer datatype and converts that object to a json

\$\endgroup\$
  • \$\begingroup\$ So, ps is valid in my book, because you're depending on the fact that there are processes running in the system. gl and gci depend on things that you can't necessarily assume, so are invalid IMO. \$\endgroup\$ – Nathan Merrill Jun 20 '17 at 2:30
1
\$\begingroup\$

Tcl, 18 bytes

puts \{"[pwd]"\:4}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl, 18 bytes

say"{'".[2]."':1}"

Prints an array reference in quotes as the name and 1 as the value. Sample output:

{'ARRAY(0x60002b5c0)':1}
\$\endgroup\$
  • 12
    \$\begingroup\$ This isn't valid JSON because strings must be delimited with double quotes (not single quotes as you currently have it). \$\endgroup\$ – PhiNotPi Oct 25 '16 at 2:56
  • \$\begingroup\$ Chrome console accepts it just fine \$\endgroup\$ – Gabriel Benamy Oct 25 '16 at 3:06
  • 5
    \$\begingroup\$ @GabrielBenamy Unfortunately, standard JSON is different from Javascript objects, and doesn't allow single quotes. Source: json.org \$\endgroup\$ – DanTheMan Oct 25 '16 at 3:34
  • \$\begingroup\$ If you're changing the quotes anyway, you can remove the 2 in the [] and it'll still be an arrayref for -1! \$\endgroup\$ – Dom Hastings Oct 25 '16 at 7:18
  • 2
    \$\begingroup\$ Another alternative: say"{\"$~\":$]}"! \$\endgroup\$ – Dom Hastings Oct 25 '16 at 7:24
0
\$\begingroup\$

C++11, 30 bytes

As unnamed lambda, port of jrich's answer, -1 byte thanks to ETHproductions

[](){printf("{\"\":%f}",9e9);}

Usage:

int main(){
 [](){printf("{\"\":%f}",9e9);}();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.