# Print some JSON

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!

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

# Bash, 18 bytes

echo {\"$PATH\":1} This works on Mac and Linux, unless your$PATH isn't set.

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

# 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.

# 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


# 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.

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

# 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!

## 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.

# 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}

• Could you provide sample output? – Nathan Merrill Oct 25 '16 at 14:09
• sure, updated post – skeevey Oct 25 '16 at 17:48

# 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}}"

• 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+'}' – caffeinated.tech Oct 26 '16 at 12:36
• @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 :) – ETHproductions Oct 26 '16 at 13:18

## 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.

TSQL(2016 only), 50 BYTES

SELECT*FROM(VALUES(GETDATE()))AS A(a)FOR JSON PATH

• You don't require the AS  for the alias and NEWID instead of GETDATE will save a couple as well. – MickyT Jun 20 '17 at 1:36

# 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?"}":","));
}
}

• 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 ==. – user62131 Jun 19 '17 at 10:19
• Good idea, I'll update the code. I don't know why but I really wanted to make a loop. – Llew Vallis Jun 19 '17 at 10:20
• 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 "\""'"'. – manatwork Jun 19 '17 at 10:26
• 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. – manatwork Jun 19 '17 at 10:35
• Welcome to PPCG! – Martin Ender Jun 19 '17 at 10:36

# 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}


# 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>

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!

# 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.

# 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è;':@'{"
}


# 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.

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

_=>{"":${9e9}}  It will run in most modern ECMAScript environments (eg. Firefox, Chrome, MS Edge, Opera, Node…). 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]". Actionscript 3, 27 bytes trace(JSON.stringify(int)); outputs {"MAX_VALUE":2147483647,"length":1,"MIN_VALUE":-2147483648,"prototype":{}} 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} • Cool. Do you need the 0 in 1.0, or will 1./0 suffice? – ETHproductions Oct 28 '16 at 23:34 • 1./0 is a syntax error in c#, namely "Identifier expected". Nice thought though :) – Travis J Oct 28 '16 at 23:55 # 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). • Doesn't seem to work the same way here: pastebin.com/P5vau3WB – manatwork Oct 31 '16 at 8:59 • @manatwork Works with QPython on my android – Noodle9 Oct 31 '16 at 10:52 • @manatwork And Python 2.6.6 but not Python 2.7.1 – Noodle9 Oct 31 '16 at 13:03 # 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()); }  # 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. # 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.

# 05AB1E, 15 13 bytes

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


Try it online!

{"abcdefghijklmnopqrstuvwxyz":"zyxwvutsrqponmlkjihgfedcba"}

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

## 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

• 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. – Nathan Merrill Jun 20 '17 at 2:30

# Tcl, 18 bytes

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


Try it online!

# 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}

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

# 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);}();
}
`