Jelly, 31 30 30 27 bytes
ŒJẈạṀ$‘Ø0jIr0Ṣ€Ṡị“[],”⁸Fż@¥FḊṖ”żFFḊṖ I'm pretty rusty in Jelly, soso can probably be golfed a bit further. It indeed can probably be golfed a bit further:
-3 bytes thanks to @JonathanAllan.
Port of my 05AB1E answer. The Ø0jIr0Ṣ€Ṡị“[],” and ż¥FḊṖ”żFFḊṖ are takenis a slight modification from @xigoi's Jelly answer for the Build a list from a depth map challenge.
Try it onlineTry it online or verify all test casesverify all test cases.
# Main link, taking the input-list as argument: ŒJẈ # Determine the depth of each integer: ŒJ # Get the multidimensional arguments Ẉ # Get the length of each inner list ạṀ$‘ # Invert this depth-list: $ # Apply the previous two links as monad: Ṁ # Get the maximum of this list of depths ạ # Calculate the absolute difference ‘ # Increase each 0-based depth by 1 Ø0jIr0Ṣ€Ṡị“[],” # Create a string to zip with: Ø0j # Surround the list with leading/trailing 0 I # Forward-differences r0 # Map each integer `n` to a list in the range [n,0] Ṣ€ # Sort each range Ṡ # Signum (-1 if <0; 0 if 0; 1 if >0) ị“[],” # Index into "[]," ("[" if 1; "," if 0; "]" if -1) ¥ # Using two lists as dyad arguments ż @ # with reversed argumentsZip/transpose, ⁸F # where the first argument isusing the flattenedlist inputabove F # and the second argument is the string above ż # flattened Zipinput themas togetherarguments F F # Then flatten the result ḊṖ ḊṖ # And remove the leading/trailing "," # (after which the result is output implicitly)