78
\$\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\$
7
  • 1
    \$\begingroup\$ I like the variety of answers on this one \$\endgroup\$ 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
  • 5
    \$\begingroup\$ @Konijn like I said, it must be a valid JSON object. The object is defined by the curly braces. \$\endgroup\$ 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\$ Feb 2 '17 at 15:05

73 Answers 73

3
\$\begingroup\$

Perl 5, 16 15 bytes

Uses the unix timestamp of the moment the program was started as the content of a one-element object. It gives valid output if you run it later than 10:46:39 on 3rd of March 1973. But since we can't go back in time that seems legit.

say"{\"$^T\":1}"

Uses the FORMAT_TOP_HANDLE variable $^ which defaults to STDOUT_TOP.

say"{\"$^\":1}";

Run with -E flag at no additional cost.

perl -E 'say"{\"$^\":1}";'

Outputs is 16 bytes long.

{"STDOUT_TOP":1}
\$\endgroup\$
2
  • \$\begingroup\$ RIP time dependent answer :( \$\endgroup\$ Jun 19 '17 at 22:50
  • \$\begingroup\$ @CalculatorFeline hu? \$\endgroup\$
    – simbabque
    Jun 19 '17 at 23:15
3
\$\begingroup\$

Cheddar, 17 14 bytes

Saved 3 bytes thanks to @ConorO'Brien

->'{":1234}'*2

An anonymous function that returns {":1234}{":1234}, which is a valid JSON object:

{
  ":1234}{": 1234
}

Try it online!

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

Windows batch, 10 bytes

{"%OS%":5}

The environment variable OS contains the string Windows_NT (On all Windows operating systems since 2000, according to this question on Stack Overflow)

\$\endgroup\$
5
  • \$\begingroup\$ @ETHproductions Oops \$\endgroup\$
    – penalosa
    Oct 25 '16 at 20:15
  • \$\begingroup\$ Is this actually outputting anything? Neil's Batch answer certainly does, but can't remember about any implicit batch output. \$\endgroup\$
    – manatwork
    Oct 26 '16 at 9:26
  • \$\begingroup\$ @manatwork Yes it does, since echo is turned on, the current command is echoed, evaluating the OS variable in the process. \$\endgroup\$
    – penalosa
    Oct 26 '16 at 16:39
  • \$\begingroup\$ Oh. So actually it tries to execute that {"%OS%":5} as it would be a command and you benefit of the elementary tracing feature? That's tricky. \$\endgroup\$
    – manatwork
    Oct 26 '16 at 16:48
  • \$\begingroup\$ @manatwork Yes, exactly. \$\endgroup\$
    – penalosa
    Oct 26 '16 at 17:07
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\$
2
  • 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
  • 2
    \$\begingroup\$ Could you provide sample output? \$\endgroup\$ 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\$
2
  • \$\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\$ 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\$ 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\$
1
  • \$\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\$
7
  • \$\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\$ 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\$ 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\$

Groovy, 43 Bytes

{groovy.json.JsonOutput.toJson([i:1..9]​)​}

It gives the range 1-9 in JSON stored for i:

{"i":[1,2,3,4,5,6,7,8,9]}

Groovy on Grails, 31 Bytes

def v(){render(params as JSON)}

Controller method, renders the parameters which always include action, controller and a few other meta parameters which results in:

{"action":"v","format":null,"controller":"<Controller Name>"}

When you visit the URL:

http://localhost:8080/<Controller Name>/v
\$\endgroup\$
2
  • \$\begingroup\$ Your program should take no input. \$\endgroup\$ Oct 25 '16 at 17:02
  • \$\begingroup\$ @NathanMerrill fixed, the only cool part to my answer is the Grails part anyway. \$\endgroup\$ Oct 25 '16 at 17:05
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\$
3
  • 2
    \$\begingroup\$ You need to either define a function, or a full program (with printing). \$\endgroup\$ 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\$
2
  • \$\begingroup\$ Cool. Do you need the 0 in 1.0, or will 1./0 suffice? \$\endgroup\$ 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\$
3
  • \$\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\$
3
  • 2
    \$\begingroup\$ Your code outputs single quotes instead of double \$\endgroup\$ Feb 2 '17 at 14:25
  • \$\begingroup\$ @user2428118 fixed at a -2byte cost. JSON with ' is parsable in some languages though. \$\endgroup\$ Feb 2 '17 at 14:57
  • 1
    \$\begingroup\$ @MagicOctopusUrn Doubt it. It's probably not actual JSON if single quotes are parsable, maybe you meant dictionaries? \$\endgroup\$
    – Makonede
    Jan 20 at 19:50
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\$
1
  • \$\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\$ Jun 20 '17 at 2:30
1
\$\begingroup\$

Prolog (SWI), 19 bytes

?-writeq({"":1e7}).

Try it online!

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

Tcl, 18 bytes

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

Try it online!

\$\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.