I am trying to decode the JSON in normal string and print that in the info log in Magento 2.4.0 but I am getting nothing in the info log. Adding the code below anyone please suggest a solution for this.

Code I am using

public function execute()
    $result = $this->resultJsonFactory->create();
    $isSuccess = $this->tracking->createApi();
    $jsonresponse = json_decode($isSuccess); // decoding here
    $this->logger->info(var_dump($jsonresponse)); // trying to print in info log
    return $result->setData(['success' => $isSuccess]);

$isSuccess - this variable has the below JSON data

Coveolog.INFO: {"sourceType":"PUSH","id":"dsspartnersorggrc3y48w-uk7awlmkozpufpe6pwaeoycmpy","name":"DSS-Coveo_M2-Integration-B2C-product-en_US","sourceVisibility":"SHARED","customParameters":{},"information":{"sourceStatus":{"type":"CREATING","timestamp":1617776580670,"allowedOperations":["DELETE"]},"numberOfDocuments":0,"documentsTotalSize":0},"pushEnabled":true,"onPremisesEnabled":false,"streamEnabled":false,"logicalIndex":"default","preConversionExtensions":[],"postConversionExtensions":[],"permissions":{"permissionLevels":[{"name":"Source Specified Permissions","permissionSets":[{"name":"Shared","permissions":[{"allowed":true,"identityType":"GROUP","identity":"@","securityProvider":"Email Security Provider"}]}]}]},"urlFilters":[{"filter":"*","includeFilter":true,"filterType":"WILDCARD"}],"ocrFileTypes":[],"resourceId":"dsspartnersorggrc3y48w-uk7awlmkozpufpe6pwaeoycmpy"} [] []

1 Answer 1

  1. You don't need to use var_dump() when logging something in a file; the function is used when you want to dump info on the browser instead.
  2. When decoding, I would suggest you use the function like this json_decode($isSuccess, true) so that your variable's content is properly converted to associative array and not object.
  • I tried as per your comment, but I am not getting the result.
    – Ajmalriyaz
    Apr 7, 2021 at 12:05
  • hmm ok, well i am not entirely sure how is your message formed, maybe i am missing something.
    – Diana
    Apr 7, 2021 at 13:05
  • I am writing this code inside the controller is that correct as per Magento standards?
    – Ajmalriyaz
    Apr 7, 2021 at 13:21
  • can you also try this? json_decode(trim($isSuccess), true);?
    – Diana
    Apr 8, 2021 at 11:53
  • this is also not working.
    – Ajmalriyaz
    Apr 8, 2021 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.