Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Conixs's avatar

Transform API return

Hello,

I'm using a third party API that returns to me some data and I need to manipulate it to build some dashboards. The dashboard expect the data to be grouped by weeks to construct the values and labels.

I'm getting the response and pushing to an array as below:

$registered = [];
array_push($registered, json_decode($response->getBody(),true));

However, the data is added as follow and I cannot manipulate it in the necessary way.

{
   "registered":[
      "[{\"IdBoleto\":17437,\"DueDate\":\"2019-01-31T00:00:00\",\"PaymentDate\":null,\"CreditDate\":null,\"PrincipalValue\":5.99,\"InterestValue\":0.00,\"FineValue\":0.00,\"TotalValue\":5.99,\"PaidValue\":0.0,\"Status\":3,\"MailToSend\":\"[email protected]\",\"PhoneToSend\":\"telefone\",\"Comments\":\"Boleto 02\",\"OurNumber\":\"000000002684\",\"Identifier\":\"Teste2\",\"ReturnMessage\":null,\"ReturnCode\":null,\"Barcode\":null,\"DigitableLine\":null,\"OccurrenceMotive\":[{\"OccurrenceCode\":\"02\",\"OccurrenceMessage\":\"Entrada Confirmada\",\"Motives\":[null]}],\"Url\":\"https://sandboxboletos.com.br/boletofd2fngmx.pdf\",\"CustomerName\":\"Felipe Nogueira Gomes\",\"CustomerTaxNumber\":\"taxNumber\",\"PayerBankCode\":null,\"PayerBankBranchCode\":null}]"
   ]
}

I need to get this and return as follow:

{
   "registered":{
      "value":[
         10,
         20,
         30
      ],
      "label":[
         1,
         2,
         3
      ],
      "total":60
   }
}

Could someone help me?

0 likes
2 replies
bobbybouwmann's avatar

It seems that the data is still json in the array. You need to make sure that you first don't have any json anymore. You can do that like so

$registered = json_decode((string) $response->getBody(), true));

Notice that we cast the response first to a string, this is the best way to convert it back to an array.

After that, you should create a collection so you can easily group, sum, etc.

Documentation: https://laravel.com/docs/6.x/collections

Conixs's avatar

It works, but it put all objects on the same position:

Collection {#959
  #items: array:1 [
    0 => "[{"IdBoleto":17406,"DueDate":"2019-01-11T00:00:00","PaymentDate":"2019-01-11T18:44:57.937","CreditDate":null,"PrincipalValue":4.50,"InterestValue":0.00,"FineValue":0.00,"TotalValue":4.50,"PaidValue":4.50,"Status":5,"MailToSend":"[email protected]","PhoneToSend":"phoneNumber","Comments":"Boleto de teste webhook","OurNumber":"000000002654","Identifier":"5","ReturnMessage":null,"ReturnCode":null,"Barcode":null,"DigitableLine":null,"OccurrenceMotive":[{"OccurrenceCode":"06","OccurrenceMessage":"PAGAMENTO EFETUADO","Motives":[null]}],"Url":"boleto.pdf","CustomerName":"customer","CustomerTaxNumber":"taxnumber","PayerBankCode":null,"PayerBankBranchCode":null},{"IdBoleto":17614,"DueDate":"2019-01-28T00:00:00","PaymentDate":"2019-01-25T13:34:31.307","CreditDate":null,"PrincipalValue":4.30,"InterestValue":0.00,"FineValue":0.00,"TotalValue":4.30,"PaidValue":4.30,"Status":5,"MailToSend":"[email protected]","PhoneToSend":"phoneNumber","Comments":null,"OurNumber":"000000002851","Identifier":"Teste06","ReturnMessage":null,"ReturnCode":null,"Barcode":null,"DigitableLine":null,"OccurrenceMotive":[{"OccurrenceCode":"06","OccurrenceMessage":"PAGAMENTO EFETUADO","Motives":[null]}],"Url":"boleto.pdf","CustomerName":"customer","CustomerTaxNumber":"taxNumber","PayerBankCode":null,"PayerBankBranchCode":null}]"
  ]
}

Please or to participate in this conversation.