xdotool getactivewindow is what you want - did you try it?
It prints the window id (from the window stack) if there are no further xdotool subcommands on the command line.
xdotool getactivewindow getwindowpid for example,
getactivewindow puts the id on the window stack, and
getwindowpid uses this id to query the PID. Note that running that command in the terminal will always return the ID of the terminal window, because it is active. In order to get the ID from another window try
sleep 2s && xdotool getactivewindow and select the window of interest in the two seconds time span.
There is a complication when using
xdotool with other tools for window handling:
xdotool output uses decimal numbers for windwo ids, most other tools use hexadecimal numbers for output (they support both for input usually).
For example, if you find a window with
xdotool getactivewindow, you will not find the result in the output of
xwininfo -root -tree, that lists all windows. It needs to be converted to a hexadecimal number first:
$ xdotool getactivewindow
$ printf 0x%x 69206716
$ xwininfo -root -tree | grep 0x42002bc
0x42002bc (has no name): ("konsole" "Konsole") 1154x781+0+0 +1289+498
Converting decimal to hexadecimal:
printf 0x%x 69206716
Converting hexadecimal to decimal:
printf %i 0x42002bc