| Bytes | Lang | Time | Link |
|---|---|---|---|
| 043 | Jsonnet | 240529T185516Z | Digital |
| 033 | Factor | 230219T094450Z | chunes |
| nan | 230219T083224Z | The Thon | |
| 036 | jq | 230219T082550Z | GammaFun |
| 021 | Raku | 221217T033630Z | naffetS |
| 023 | FunStack alpha | 221217T014228Z | DLosc |
| 035 | GNU sed | 221213T074859Z | seshouma |
| 053 | R | 221215T111929Z | Dominic |
| 021 | Julia 1.0 | 221215T121933Z | MarcMush |
| 005 | 05AB1E | 221213T081640Z | Kevin Cr |
| 032 | TIBasic | 221215T125413Z | MarcMush |
| 022 | Retina | 221212T224607Z | Neil |
| 022 | JavaScript Node.js | 221212T204405Z | Conor O& |
| 015 | sclin | 221212T232609Z | Mama Fun |
| 064 | PHP | 221213T124629Z | Kaddath |
| 058 | ><> | 221213T075722Z | mousetai |
| 004 | Vyxal | 221213T054927Z | u-ndefin |
| 8210 | QuadR | 221213T052621Z | Adá |
| 006 | APL Dyalog Extended | 221212T203646Z | Adá |
| 029 | Python | 221213T000953Z | 97.100.9 |
| 009 | Jelly | 221213T004845Z | Jonathan |
| 011 | Charcoal | 221212T225519Z | Neil |
| 076 | Batch | 221212T225108Z | Neil |
Jsonnet, 43
local f(l)=if l>0then[f(l-1),f(l-1)]else[];
Defines a jsonnet function - the nesting level is passed as a parameter.
Thunno, \$7\log_{256}(96)\approx\$ 5.76 bytes
ls{KDZP
Port of Kevin Cruijssen's 05AB1E answer.
Explanation
ls{KDZP # Implicit input
ls # Push an empty list and swap
{K # Repeat (input) times:
DZP # Duplicate and pair
# Implicit output
jq, 36 bytes
def n:try(.<0//(.-1|[n,n]))+[]//[];n
Thanks to ovs for the try(A//C)+[]//B hack!
Our recursive function has a base case of -1, which yields []. Otherwise, our input is decremented, and [n,n] recurses into the function twice.
FunStack alpha, 23 bytes
Pair self iterate "" At
You can try it at Replit. Takes the depth (1-indexed) as a command-line argument and the program on stdin.
Explanation
At the core of this solution is the following infinite sequence:
[]
[[],[]]
[[[],[]],[[],[]]]
...
We start with the empty list, and each subsequent element is two copies of the previous element wrapped in a list. Pair self does exactly that, and we get the desired infinite list by iterateing that function starting from "" (empty string / empty list).
This creates a bare value at the left end of the program, so it is appended to the program's argument list. Then At takes the first argument as an index into the second argument and returns the corresponding element.
GNU sed, 36 35 bytes
I believe numeric input is allowed to be in unary format for sed. For ex. 3 becomes @@@ and 0 becomes the empty string (no @s). The nameless label : is supported by older versions of sed.
s:$:@[]:
:
s:@::
s:\[]:[&, &]:g
/@/b
In each loop iteration one character is removed from the unary number as one depth is introduced in the array. A small trick is used in the first line, where the initialization is only [] (shorter than [[], []]), like a "-1 based indexing". As such the input number is incremented by one with the extra @. This helps also with not checking up front for the empty string (input 0).
EDIT: with 1-based indexing now allowed, the incrementing from first line isn't needed,thus saving one byte: s:$:[]: Thanks to Adám for the heads-up.
R, 57 53 bytes
Edit: saved 4 bytes by copying MarcMush's approach
\(n){s="[]";for(i in 1:n)s=paste0("[",s,",",s,"]");s}
Returns JSON string.
R, 41 37 bytes
f=\(n,s={})`if`(n,f(n-1,list(s,s)),s)
Returns R nested list, which can be converted into a json string.
Julia 1.0, 21 bytes
!x=1:2(x>0).|>_->!~-x
returns a nested array
Explanation
a .|> fappliesfon each element ofa- when
x=0,1:0is (kinda) an empty list, so the result is an empty list - when
x>0,1:2acts like a 2-element list, and each element of this list will be!(x-1)
05AB1E, 5 bytes
¯IƒD‚
Try it online or verify all test cases.
Explanation:
¯ # Push an empty list: []
Iƒ # Loop the input+1 amount of times:
D # Duplicate the current list
‚ # Pair the two lists together
# (after the loop, output the result implicitly)
Retina, 22 bytes
K`[]
"$+"+`\[]
[[],[]]
Try it online! No test suite due to the way the program uses history. Explanation:
K`[]
Replace the input with an empty array.
"$+"+`
Repeat n times...
\[]
[[],[]]
... replace each empty array with a pair of empty arrays.
Previous 0-indexed version was 27 bytes:
K`[[],[]]
"$+"+`\[]
[[],[]]
Try it online! No test suite due to the way the program uses history. Explanation: Starts with the first pair of empty arrays thus reducing the number of iterations needed by 1.
JavaScript (Node.js), 25 23 22 bytes
Saved 2 bytes thanks to Arnauld! Saved 1 byte by changing to 1-indexed.
f=n=>n--?[a=f(n),a]:[]
Try it online! Arnauld's observation of testing for 1-indexed as ~n-- saves 1 byte over n--+1.
Returns an array of arrays, which, when printed, will output JSON compliant arrays.
If a string output is mandatory, then we have, using the same method:
JavaScript (Node.js), 34 32 31 29 bytes
Saved 2 bytes thanks to Arnauld! Saved 2 bytes thanks to l4m2! Saved 1 byte by changing to 1-indexed.
f=n=>`[${n--?[a=f(n),a]:[]}]`
sclin, 15 bytes
[]"dup ,";1+ *#
Try it here! I've been thinking about having code that took input from the next line rather than the previous. Surprisingly useful, gonna have to use it more often...
For testing purposes:
[]"dup ,";1+ *# f>o
2
Explanation
Prettified code:
[] ( dup , ) ; 1+ *#
[]empty list(...) ; 1+ *#execute (next line) + 1 times...dup ,duplicate and pair into list
PHP, 64 bytes
for($a=[],$b=[&$a,&$a];$argn--;)$a=[$a,$a];echo json_encode($b);
I thought this solution by reference elegant enough to be posted, I wish we could use a reference to a variable while initializing it, but unfortunately it's not the case. Too bad for the output format, almost a third of the code is lost to formatting..
><>, 58 bytes
e0i1+:?v"]["oo~.
3[r]40.\"["o:1-20
.","o:1-303[r]40
."]"o~
Offsetting the number by 1 would save 2 bytes, since I can skip the 1+.
Explanation
Top row: Main function. Check if the recursion level is 0, if so print [] and return, else go down.
Second row: print [, then recurse.
Third row: print , then recurse
Print ] and return.
Vyxal, 4 bytes
›(W:
Try it online! One-indexed. Returns a nested list.
› # increment
( # repeat for n+1 times (n+1 is popped):
W # wrap stack into single list (initially [])
: # duplicate
# implicit output
Vyxal W, 3 bytes
(W:
Here, it repeats n times, and before outputting implicitly, the W flag wraps the stack into a single list.
QuadR, 8+2=10 bytes
Takes n as 1-indexed argument on TIO.
.+
[&,&]
Initial input:
[]
.+ match everything (any number of any characters)
[&,&] replace with open-bracket, match, comma, match, close-bracket
APL (Dyalog Extended), 12 6 bytes
−6 thanks to OP now allowing 1-indexing and results that can be converted to JSON, rather than the JSON itself.
Full program; prompts for 1-indexed n and prints APL nested array equivalent of the required JSON. Adding an print handler which just converts output to JSON shows the required result.
⍮⍨⍣⎕⊢⍬
⍬ empty list ([])
⍣⎕⊢ on that, apply the following function n (prompted-for) number of times:
⍮⍨ pair up with itself
The print handler:
Print←{…} assign a function as follows:
⎕JSON⍺ convert output array to JSON
⎕← print that
Setting up the JSON printing callback on output:
⎕SE. for the current session:
onSessionPrint← set the event handler for printing to
Print call the above handler function
Python, 29 bytes
g=lambda n:n*[0]and[g(n-1)]*2
1-indexed. Returns Python's native array representation.
Python, 45 bytes
g=lambda n:n and f'[{g(n-1)},{g(n-1)}]'or'[]'
Returns a string, also 1-indexed. I found three different 45-byte functions for this, so I chose the simplest.
Jelly, 9 bytes
’ßWƊR‘?;`
A recursive, monadic Link that accepts a non-negative integer (0-indexed) and yields the nested list. (Replace ‘ with ¹ to use 1-indexed input.)
How?
’ßWƊR‘?;` - Link: integer, n
? - if...
‘ - ...condition: increment (i.e. n != -1?)
Ɗ - ...then: last three links as a monad - f(n):
’ - decrement (n) -> n-1
ß - call this Link with n-1
W - wrap that in a list
R - ...else: range (n = -1) -> []
` - use as both arguments of:
; - concatenate
Charcoal, 11 bytes
F⊕N≔E²υυ⭆¹υ
Try it online! Link is to verbose version of code. Explanation: Port of Adám's APL answer.
F⊕N
Loop n+1 times...
≔E²υυ
... replace the predefined empty list with a list of two copies of it.
⭆¹υ
Pretty-print the final list (needed because Charcoal doesn't normally output anything for empty lists).
Batch, 76 bytes
@set s=[]
@for /l %%i in (0,1,%1)do @call set s=%%s:[]=[[],[]]%%
@echo %s%
Explanation: Port of the 1-indexed version of my Retina answer, but looping from 0 to n to convert back to 0-indexed. call set is needed because otherwise the variable gets expanded before the for loop executes.

