This question is a follow-up to this question. I am trying to prove that
$f(x) = \begin{cases} \dfrac{1}{b}\; \text{ if $x$ is rational and $x= \dfrac{a}{b}$ is in its lowest terms}\\ 34 \; \text{ $x$ is irrational} \end{cases}$
is Borel measurable. I had 2 ideas- the first was to show that this function is a composition of two Borel-measurable functions and the other was to express this function as a sum/product of two Borel-measurable functions. I couldn't develop either of these ideas fully. How can I prove that this function is Borel-measurable?
Let $g=f|_{\mathbb{Q}}$ and $h=f|_{\mathbb{Q}^{c}}$, $h$ is a constant function and hence continuous with respect to subspace topology $\mathbb{Q}^{c}$, and we see that for any open set $\mathcal{O}$, \begin{align*} f^{-1}(\mathcal{O})&=\{x\in\mathbb{Q}:f(x)\in\mathcal{O}\}\cup\{x\in\mathbb{Q}^{c}:f(x)\in\mathcal{O}\}\\ &=g^{-1}(\mathcal{O})\cup h^{-1}(\mathcal{O}). \end{align*} $h^{-1}(\mathcal{O})$ is open with respect to $\mathbb{Q}^{c}$, and hence a Borel set.
We just need to justify that $g$ is Borel. But this is almost obvious since $g^{-1}(\{1/b\})$ is a discrete set for each $b\in\mathbb{Z}$. In fact, \begin{align*} g^{-1}(\mathcal{O})=\bigcup_{b\in\mathbb{Z},1/b\in\mathcal{O}}g^{-1}(\{1/b\}). \end{align*}