Here is a function that swaps the bodies and sets the new joints.
--# Main
-- physics
-- Use this function to perform your initial setup
function setup()
    c1 = physics.body(CIRCLE,25)
    c1.position = vec2(200,500)
    c2 = physics.body(CIRCLE,25)
    c2.position = vec2(200,450)
    c3 = physics.body(CIRCLE,25)
    c3.position = vec2(200,400)
    c4 = physics.body(CIRCLE,25)
    c4.position = vec2(250,450)
    physics.joint(DISTANCE,c1,c2,c1.position,c2.position)
    physics.joint(DISTANCE,c2,c3,c2.position,c3.position)
    physics.joint(DISTANCE,c2,c4,c2.position,c4.position)
    d1 = physics.body(CIRCLE,25)
    d1.position = vec2(600,500)
    d2 = physics.body(CIRCLE,25)
    d2.position = vec2(600,450)
    d3 = physics.body(CIRCLE,25)
    d3.position = vec2(600,400)
    physics.joint(DISTANCE,d1,d2,d1.position,d2.position)
    physics.joint(DISTANCE,d2,d3,d2.position,d3.position)
    c1.active = false
    c2.active = false
    c3.active = false
    c4.active = false
    d1.active = false
    d2.active = false
    d3.active = false
    parameter.action("Swap", swap)
    body1 = {c1}
    body2 = {d1}
    getAllConnected(body1, c1)
    getAllConnected(body2, d1)
end
function swap()
    print("Joints Before Swap")
    print("c2")
    for _,v in pairs(c2.joints) do
        local b
        if c2 == v.bodyA then
            b = v.bodyB
        else
            b = v.bodyA
        end
        print(b.x,b.y)
    end
    print("d2")
    for _,v in pairs(d2.joints) do
        local b
        if d2 == v.bodyA then
            b = v.bodyB
        else
            b = v.bodyA
        end
        print(b.x,b.y)
    end
    swapBodies(c2,d2)
    print("Joints After Swap")
    print("c2")
    for _,v in pairs(c2.joints) do
        local b
        if c2 == v.bodyA then
            b = v.bodyB
        else
            b = v.bodyA
        end
        print(b.x,b.y)
    end
    print("d2")
    for _,v in pairs(d2.joints) do
        local b
        if d2 == v.bodyA then
            b = v.bodyB
        else
            b = v.bodyA
        end
        print(b.x,b.y)
    end
end
-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    -- This sets the line thickness
    noStroke()
    -- Do your drawing here
    fill(0, 0, 255, 255)
    for _,v in pairs(body1) do
        ellipse(v.x, v.y, 15)
    end
    fill(255, 0, 0, 255)
    for _,v in pairs(body2) do
        ellipse(v.x, v.y, 15)
    end
end
--# Swap
function swapBodies(b1,b2)
    joints1 = {}
    joints2 = {}
    
    for _,v in pairs(b1.joints) do
        local b
        if b1 == v.bodyA then
            b = v.bodyB
        else
            b = v.bodyA
        end
        table.insert(joints1, b)
        v:destroy()
    end
    
    for _,v in pairs(b2.joints) do
        local b
        if b2 == v.bodyA then
            b = v.bodyB
        else
            b = v.bodyA
        end
        table.insert(joints2, b)
        v:destroy()
    end
    
    b1.position,b2.position = b2.position,b1.position
    
    for _,v in pairs(joints1) do
        physics.joint(DISTANCE,b2,v,b2.position,v.position)
    end
    
    for _,v in pairs(joints2) do
        physics.joint(DISTANCE,b1,v,b1.position,v.position)
    end
end
function getAllConnected(bodies, b)
    for _,v in pairs(b.joints) do
        local intable = false
        for _,vv in pairs(bodies) do
            if v.bodyB == vv then
                intable = true
            end
        end
        if not intable then
            table.insert(bodies, v.bodyB)
            getAllConnected(bodies, v.bodyB)
        end
    end
end
The swapBodies function does the work and the swap function verifies it. getAllConnected is now only used to draw the bodies.