如何解码App Store Connect销售报告并转为JSON/文本格式?
Hey there! Let me walk you through solving both of your problems with the App Store Connect Sales Reports API—getting readable content in Postman and decoding JSON-formatted reports.
1. Making Postman Read the Encoded Response (Convert to Text/JSON)
First off, the reason you’re seeing unreadable content is that App Store Connect returns most sales reports gzip-compressed by default. Here’s how to fix this:
Option 1: Enable Automatic Decompression in Postman
This is the quickest fix:
- Open your request in Postman, switch to the Settings tab (right next to Headers/Body).
- Find the option Automatically decode gzip/deflate responses and check the box.
- Resend the request—Postman will uncompress the response, and you’ll see the raw text (usually TSV/CSV format, not JSON, for most standard sales report types).
Option 2: Manually Decompress with Postman Scripts
If automatic decompression fails, or you want to process content programmatically, use Postman’s built-in pako library for gzip decompression in the Test tab:
// Grab the raw compressed response data const rawResponse = pm.response.raw; // Use pako to inflate the gzip content into readable text const pako = require('pako'); const decompressedText = pako.inflateRaw(rawResponse, { to: 'string' }); // Log the readable report to Postman's console console.log("Decompressed Sales Report:", decompressedText); // Or save it to an environment variable for easy access later pm.environment.set("readable_sales_report", decompressedText);
Quick Note on Report Formats
Most standard sales reports (like SALES type) return TSV/CSV, not JSON. If you specifically want JSON output, add the filter[format]=JSON parameter to your API request—this works for certain report types like subscription event reports.
2. Decoding App Store Connect JSON Sales & Trends Reports
If you’re working with a JSON-formatted report (either by requesting it directly or converting TSV/CSV to JSON), here’s how to parse and make sense of it:
Requesting JSON Directly
First, ensure your API request includes the filter[format]=JSON parameter. For example, your request URL might look like:
https://api.appstoreconnect.apple.com/v1/salesReports?filter[reportType]=SUBSCRIPTION_EVENT&filter[reportSubType]=SUMMARY&filter[format]=JSON&filter[frequency]=DAILY&filter[reportDate]=2024-05-01
Parsing the JSON Response
The JSON structure maps directly to Apple’s documented fields. Key details to keep in mind:
- Date fields: Use ISO 8601 format (e.g.,
2024-05-01T00:00:00Z), which you can parse with standard date functions in most programming languages. - Monetary values: Amounts are returned as integers in millidollars (e.g.,
1999equals $19.99). Divide by 1000 to get the actual dollar amount. - Event types: For subscription reports, fields like
eventTypewill map to values such asINITIAL_PURCHASE,RENEWAL, orCANCEL.
Converting TSV/CSV to JSON (If Needed)
If you have a TSV report and want to convert it to JSON in Postman, add this script to the Test tab (after decompressing the content):
// Retrieve the decompressed TSV text from the environment variable const tsvText = pm.environment.get("readable_sales_report"); // Split text into lines, skipping empty ones const lines = tsvText.split('\n').filter(line => line.trim() !== ''); // Extract column headers from the first line const headers = lines[0].split('\t').map(header => header.trim()); // Convert each data line to a JSON object const jsonReport = lines.slice(1).map(line => { const values = line.split('\t').map(val => val.trim()); return headers.reduce((obj, header, index) => { obj[header] = values[index]; return obj; }, {}); }); // Log the formatted JSON to the console console.log("JSON Formatted Report:", JSON.stringify(jsonReport, null, 2)); // Save the JSON to an environment variable pm.environment.set("sales_report_json", JSON.stringify(jsonReport, null, 2));
内容的提问来源于stack exchange,提问作者Ravi Agrawal

