Prepare JSON passing & get all device ids

Each of these lines repeats the same thing twice. Is it possible to clean up? Here and below.