selling-partner-api icon indicating copy to clipboard operation
selling-partner-api copied to clipboard

Report GET_MERCHANT_LISTINGS_ALL_DATA in Json

Open elvispdosreis opened this issue 1 year ago • 1 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();

elvispdosreis avatar Dec 19 '23 15:12 elvispdosreis

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

mewthu2 avatar Jan 23 '24 19:01 mewthu2

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)

jlevers avatar Mar 28 '24 21:03 jlevers