g | x | w | all
Bytes Lang Time Link
043Jsonnet240529T185516ZDigital
033Factor230219T094450Zchunes
nan230219T083224ZThe Thon
036jq230219T082550ZGammaFun
021Raku221217T033630ZnaffetS
023FunStack alpha221217T014228ZDLosc
035GNU sed221213T074859Zseshouma
053R221215T111929ZDominic
021Julia 1.0221215T121933ZMarcMush
00505AB1E221213T081640ZKevin Cr
032TIBasic221215T125413ZMarcMush
022Retina221212T224607ZNeil
022JavaScript Node.js221212T204405ZConor O&
015sclin221212T232609ZMama Fun
064PHP221213T124629ZKaddath
058><>221213T075722Zmousetai
004Vyxal221213T054927Zu-ndefin
8210QuadR221213T052621ZAdá
006APL Dyalog Extended221212T203646ZAdá
029Python221213T000953Z97.100.9
009Jelly221213T004845ZJonathan
011Charcoal221212T225519ZNeil
076Batch221212T225108ZNeil

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.

Online Jsonnet validator

Factor, 33 bytes

[ { } swap [ dup 2array ] times ]

Try it online!

Thunno, \$7\log_{256}(96)\approx\$ 5.76 bytes

ls{KDZP

Attempt This Online!

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

Try it online!

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.

Raku, 21 bytes

{(@,{$_,$_}...*)[$_]}

Try it online!

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

Try it online with sed 4.2.2!

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.

Attempt This Online!


R, 41 37 bytes

f=\(n,s={})`if`(n,f(n-1,list(s,s)),s)

Attempt This Online!

Returns R nested list, which can be converted into a json string.

Julia 1.0, 21 bytes

!x=1:2(x>0).|>_->!~-x

Try it online!

returns a nested array

Explanation

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)

TI-Basic, 32 bytes

Prompt N
"[]
For(I,0,N
"["+Ans+","+Ans+"]
End
Ans

enter image description here

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]:[]}]`

Try it online!

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

PHP, 64 bytes

for($a=[],$b=[&$a,&$a];$argn--;)$a=[$a,$a];echo json_encode($b);

Try it online!

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~

Try it

Offsetting the number by 1 would save 2 bytes, since I can skip the 1+.

Explanation

enter image description here

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:

Try it online!

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:

[]

Try it online!

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

⍮⍨⍣⎕⊢⍬

Try it online!

 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

Attempt This Online!

1-indexed. Returns Python's native array representation.

Python, 45 bytes

g=lambda n:n and f'[{g(n-1)},{g(n-1)}]'or'[]'

Attempt This Online!

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

Try it online!

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.