most surprising to me is that if you run through the browser then everything is done.
But when I run through the console, it gets a fatal error pointing to this line of code.
Why have you decided that they should have identical configs? On the contrary, the configs are usually different.
Well, error has no relation to json. If the memory limit is memory_limit, allocated by the PHP engine. Wherever you don't exceed it - get a fatal error, it's simple. And from here only two: try to reduce the memory consumption of the script or raise the limit.