Let's denote $Y = E(X|\mathfrak{G})$

$\color{blue}{\text{Step 1:}}$ let's suppose $X \in L^2$, then since $EX^2 = EY^2$, we have \begin{align} E\left(X - Y\right)^2 =& E(X^2) - 2E(XY) + E(Y^2) \\ =& EX^2 + EY^2 - 2E\left(E(XY|\mathfrak{G})\right) \\ =& EX^2 + EY^2 - 2E\left(YE(X|\mathfrak{G})\right) \\ =& EX^2 + EY^2 - 2EY^2 = 0 \end{align}

so $X=Y$ almost surely.

$\color{blue}{\text{Step 2:}}$ Now we remove the assumption $X \in L^2$. Then we need to consider $X' = X\wedge a \vee b$ and $Y' = Y\wedge a \vee b$, i.e. the truncated version of $X$ and $Y$. We will see $X' = Y'$ almost surely, then by sending $a \to +\infty$ and $b\to -\infty$, we see $X = Y$ almost surely.

To prove $X'= Y'$ almost surely, we will prove $E(X'|\mathfrak{G}) = Y'$, then since $X'$ and $Y'$ still have the same distribution, by $\color{blue}{\text{Step 1}}$ we see $X' = Y'$ almost surely.

So all we need to do now is to prove $$E(X'|\mathfrak{G}) = Y'$$

Firstly of all, $Y'$ is $\mathfrak{G}$-measurable.

Then by Jensen's inequality applied on conditional expectation, we have $$E(X\wedge a|\mathfrak{G}) \leq E(X|\mathfrak{G})\wedge a = Y\wedge a$$

However, since $E(X\wedge a) = E(Y\wedge a)$, the above inequality can't be strict on a set of positive probability, so we get

$$E(X\wedge a|\mathfrak{G}) = Y\wedge a$$

By a similar argument, we get

$$E(X\wedge a \vee b|\mathfrak{G}) = Y\wedge a\vee b$$


Here, the main difficulty is that we do not assume finiteness of the expectation of $X^2$.

Fix a real number $x$ and define $A:=\{X\leqslant x\}$ and $B:=\{\mathbb E[X\mid\mathcal G]\leqslant x\}$. Using the assumption, we have $$\mathbb E[X\chi(A)]=\mathbb E[X\chi(B)].$$

Indeed, since $B$ belongs to $\mathcal G$, we have $$\mathbb E[X\chi(B)]=\mathbb E[\mathbb E[X\mid\mathcal G]\chi(B)] =\mathbb E[\mathbb E[X\mid\mathcal G]\chi\{\mathbb E[X\mid\mathcal G]\leqslant x\}],$$ and the random variables $X\chi\{X\leqslant x\}$ and $\mathbb E[X\mid\mathcal G]\chi\{\mathbb E[X\mid\mathcal G]\leqslant x\}$ have the same distribution.

Define $C_1:=A\setminus B$ and $C_2:=B\setminus A$. Since $\mathbb P(A)=\mathbb P(B)$, we have $$\mathbb E\left[(X-x)\chi(C_1)\right]=\mathbb E[(X-x)\chi(C_2)].$$ As $(X-x)\chi(C_1)\leqslant 0\leqslant (X-x)\chi(C_2)$, we get that $\mathbb P(A\Delta B)=0$. Define $A':=\{X\geqslant -x\}$ and $B':=\{\mathbb E[X\mid\mathcal G]\geqslant -x\}$. By the argument uses with $-X$ instead of $X$, we get $\mathbb P(A'\Delta B')=0$. Defining $A'':=A\cap A'$ and $B'':=B\cap B'$, we have $\mathbb P(A''\Delta B'')=0$ hence $$\mathbb E[\left(\mathbb E[X\mid\mathcal G]\right)^2\chi(A'')]=\mathbb E[\left(\mathbb E[X\mid\mathcal G]\right)^2\chi(B'')]=\mathbb E[X^2\chi(|X|\leqslant x)]$$ and $$\mathbb E\left[X\mathbb E[X\mid\mathcal G]\chi(A'')\right]=\mathbb E[\left(\mathbb E[X\mid\mathcal G\right)^2\chi(B'')],$$ hence $$\mathbb E\left[\left(X-\mathbb E[X\mid\mathcal G]\right)^2\chi\{|X|\leqslant x\}\right]=0.$$ As $x$ is arbitrary, the conclusion follows.