jlevers / selling-partner-api

A PHP client library for Amazon's Selling Partner API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Report GET_MERCHANT_LISTINGS_ALL_DATA in Json

elvispdosreis opened this issue · comments

I am having difficulty bringing a report in json, is it possible to bring the data converted into json?

$reportType = [
  'contentType' => 'application/json',
  'name' => 'GET_MERCHANT_LISTINGS_ALL_DATA'
];
$reportResultDocument = $this->client_report->getReportDocument($id);
$docToDownload = new Document($reportResultDocument, $reportType);
$contents = $docToDownload->download();
$data = $docToDownload->getData();

Hello Elvis,

`function parse_data($api_response) {
$lines_2 = explode("\n", $api_response);
$header_2 = explode("\t", $lines_2[0]);

$result_hash = array();

for ($i = 1; $i < count($lines_2); $i++) {
    $values_2 = explode("\t", $lines_2[$i]);
    $listing_id = $values_2[array_search("listing-id", $header_2)];
    $data_hash_2 = array();

    foreach ($header_2 as $index => $key) {
        $data_hash_2[$key] = $values_2[$index];
    }

    $result_hash[$listing_id] = $data_hash_2;
}

return $result_hash;

}
`

api_response is a response from a get_request to url of relatory

Hi @elvispdosreis -- Amazon doesn't have any mechanism for downloading non-JSON reports as JSON, nor does this library, but since the GET_MERCHANT_LISTINGS_ALL_DATA is tabular, you should be able to do something like this:

$json = [];
foreach ($data as $row) {
    $rowObj = [];
    foreach ($row as $header => $value) {
        $rowObj[$header] = $value;
    }
    $json[] = $rowObj;
}

json_encode($json);

(I haven't tested this, so there may be issues with it, but it should point you in the right direction)