83
\$\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\$ Commented Oct 25, 2016 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
    Commented Oct 25, 2016 at 13:01
  • 7
    \$\begingroup\$ @Konijn like I said, it must be a valid JSON object. The object is defined by the curly braces. \$\endgroup\$ Commented Oct 25, 2016 at 14:01
  • 1
    \$\begingroup\$ Got it, with stress on object ;) \$\endgroup\$
    – Konijn
    Commented Oct 25, 2016 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\$ Commented Feb 2, 2017 at 15:05

84 Answers 84

3
\$\begingroup\$

Element, 18 bytes

\{\"a\"\:`9 9^`\}`

Try it online! Outputs {"a":387420489}

This is only three bytes shorter than the naive solution. The first half of the output is hard-coded (too many escape characters to make any other approach feasible), while the value is calculated as 9^9 to make a number long enough.

\$\endgroup\$
3
\$\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\$
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\$

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\$
3
\$\begingroup\$

x86-64 machine code, 16 bytes

B8 7B 22 3A 20 AB AB AB AB C7 07 30 7D 7D 00 C3

Try it online!

Produces {": {": {": {": 0}}.

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI a memory address at which to place the result, as a null-terminated byte string.

In assembly:

f:  mov eax, '{' | '"'<<8 | ':'<<16 | ' '<<24
            # Put this value in EAX.
    stosd   # Write it to the output string, advancing the pointer.
    stosd   # Again.
    stosd   # Again.
    stosd   # Again.
    mov DWORD PTR [rdi], '0' | '}'<<8 | '}'<<16 | 0<<24
            # Add this value to the output string.
    ret     # Return.
\$\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\$
2
  • 1
    \$\begingroup\$ Useless? Why? You can still refer that value by its key and use it: pastebin.com/RqbbUBEt \$\endgroup\$
    – manatwork
    Commented Oct 25, 2016 at 10:33
  • \$\begingroup\$ I guess so. If it is considered acceptable then I'll edit my answer accordingly. \$\endgroup\$
    – ElPedro
    Commented Oct 25, 2016 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

[email protected] .Q.w`;

Sample output:

q)[email protected] .Q.w`;
{"used":127392,"heap":67108864,"peak":67108864,"wmax":0,"mmap":0,"mphy":16735457280,"syms":585,"symw":18925}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Could you provide sample output? \$\endgroup\$ Commented Oct 25, 2016 at 14:09
  • \$\begingroup\$ sure, updated post \$\endgroup\$
    – skeevey
    Commented Oct 25, 2016 at 17:48
  • \$\begingroup\$ You can just pass the entire .q namespace to .j.j for 7 bytes. This returns a JSON object: .j.j .q \$\endgroup\$ Commented Nov 28, 2022 at 6:06
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\$ Commented Oct 26, 2016 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\$ Commented Oct 26, 2016 at 13:18
2
\$\begingroup\$

Actionscript 3, 27 bytes

trace(JSON.stringify(int));

outputs

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

\$\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
    Commented Jun 20, 2017 at 1:36
2
\$\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\$ Commented Jun 20, 2017 at 2:30
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
    Commented Jun 19, 2017 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\$ Commented Jun 19, 2017 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
    Commented Jun 19, 2017 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
    Commented Jun 19, 2017 at 10:35
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Commented Jun 19, 2017 at 10:36
2
\$\begingroup\$

Vim, 13 keystrokes

a{"<esc>9aa<esc>a":0}

Outputs

{"aaaaaaaaa":0}
\$\endgroup\$
2
\$\begingroup\$

K (ngn/k), 12 bytes

`0:`j@`a!1e9

Try it online!

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

Go, 47 bytes

import."fmt"
func f(){Print(`{"X":10,"Y":20}`)}

Attempt This Online!

To STDERR, 32 bytes

func(){print(`{"X":10,"Y":20}`)}

Attempt This Online!

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

jq -n, 12 bytes

""|match("")

Try it online!

env does not always satisfy the requirement. It depends on the environment, which could even be empty, outputting {}. This solution yields the same result every time:

{
  "offset": 0,
  "length": 0,
  "string": "",
  "captures": []
}
\$\endgroup\$
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\$ Commented Oct 25, 2016 at 17:02
  • \$\begingroup\$ @NathanMerrill fixed, the only cool part to my answer is the Grails part anyway. \$\endgroup\$ Commented Oct 25, 2016 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\$ Commented Oct 26, 2016 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
    Commented Oct 31, 2016 at 8:46
  • \$\begingroup\$ I guess it's a REPL? \$\endgroup\$
    – l4m2
    Commented Apr 20, 2018 at 12:46
1
\$\begingroup\$

RAGE!!!, 41 bytes

<rant>SCREAM"{\"12345\":54321}"!!!</rant>

Since the interpreter compiler transpiler on the esolangs page converts the code to Python 2 via mere string substitution, escaping double quotes should be okay.

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

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\$ Commented Oct 28, 2016 at 23:34
  • \$\begingroup\$ 1./0 is a syntax error in c#, namely "Identifier expected". Nice thought though :) \$\endgroup\$
    – Travis J
    Commented Oct 28, 2016 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
    Commented Oct 31, 2016 at 8:59
  • \$\begingroup\$ @manatwork Works with QPython on my android \$\endgroup\$
    – Noodle9
    Commented Oct 31, 2016 at 10:52
  • \$\begingroup\$ @manatwork And Python 2.6.6 but not Python 2.7.1 \$\endgroup\$
    – Noodle9
    Commented Oct 31, 2016 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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