Unexpected file structure - expected JSONL

Solved

Unexpected file structure - expected JSONL

JayPRajput
Shopify Partner
5 1 0

Hi guy's 
I am working on bulk operation because I want to create bulk products using graph sql api 
but i am getting errror Unexpected file structure And I have read all document and i develop code as per document and try to find solution but not getting any solution could some one help me?

I am adding here all my code 

$shopifyStore = 'mystorexxx.myshopify.com';
$accessToken = 'xxxxxxx'

$graphqlQuery = <<<GRAPHQL
mutation stagedUploadsCreate(\$input: [StagedUploadInput!]!) {
stagedUploadsCreate(input: \$input) {
stagedTargets {
url
resourceUrl
parameters {
name
value
}
}
}
}
GRAPHQL;
$graphqlVariables = [
"input" => [
[
"filename" => "jptest.jsonl", // this file i have also add like this 
"mimeType" => "text/jsonl",
"httpMethod" => "POST",
"resource" => "BULK_MUTATION_VARIABLES",
]
],
];

$requestData = [
'query' => $graphqlQuery,
'variables' => $graphqlVariables,
];

$stagedUploadResponse = stagedUploadsCreateData($requestData);
$stagedUploadData = json_decode($stagedUploadResponse, true);

if ($stagedUploadData === null) {
echo "Error decoding JSON response.\n";
echo "JSON Error: " . json_last_error_msg() . "\n";
echo "Response received:\n";
echo $stagedUploadResponse . "\n";
} else {
// Get the first staged target from the response
$stagedTarget = $stagedUploadData['data']['stagedUploadsCreate']['stagedTargets'][0];

// Initialize cURL session for downloading the file
$chDownload = curl_init($fileUrl);
$localFilePath = "/tmp/bulk.json";
curl_setopt($chDownload, CURLOPT_RETURNTRANSFER, 1);
$fileContent = curl_exec($chDownload);
if (curl_errno($chDownload)) {
echo 'Error downloading the file: ' . curl_error($chDownload);
curl_close($chDownload);
die;
}
curl_close($chDownload);
if (file_put_contents($localFilePath, $fileContent) === false) {
echo 'Error saving the file to ' . $localFilePath;
die;
}

$fileContents = file_get_contents($localFilePath);
echo 'File contents:' . PHP_EOL;
echo $fileContents . PHP_EOL;
$chUpload = curl_init();

// Set cURL options for uploading
curl_setopt($chUpload, CURLOPT_URL, $stagedTarget['url']);
curl_setopt($chUpload, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chUpload, CURLOPT_POST, true);
$postData = [
'key' => $stagedTarget['parameters'][3]['value'],
'x-goog-credential' => $stagedTarget['parameters'][5]['value'],
'x-goog-algorithm' => $stagedTarget['parameters'][6]['value'],
'x-goog-date' => $stagedTarget['parameters'][4]['value'],
'x-goog-signature' => $stagedTarget['parameters'][7]['value'],
'policy' => $stagedTarget['parameters'][8]['value'],
'acl' => $stagedTarget['parameters'][2]['value'],
'Content-Type' => $stagedTarget['parameters'][0]['value'],
'success_action_status' => $stagedTarget['parameters'][1]['value'],
'file' => '@' .$localFilePath,
];
curl_setopt($chUpload, CURLOPT_POSTFIELDS,$postData);
$response = curl_exec($chUpload);
if (curl_errno($chUpload)) {
echo 'Error uploading the file: ' . curl_error($chUpload);
} else {

$xml = simplexml_load_string($response);
$stagedFileUrl = (string) $xml->Key;

// Output the staged file URL
echo 'Staged File URL: ' . $stagedFileUrl . PHP_EOL;
if(isset($stagedFileUrl)){
sendGraphQLRequest($stagedFileUrl);
}
}
curl_close($chUpload);
}

// Function to send GraphQL request
function sendGraphQLRequest($stagedUploadResourceUrl) {
// GraphQL query for bulkOperationRunMutation
$product_create_query ='mutation {
bulkOperationRunMutation(
mutation: "mutation call($input: ProductInput!) { productCreate(input: $input) { product {title productType vendor} userErrors { message field } } }",
stagedUploadPath: "' .$stagedUploadResourceUrl . '") {
bulkOperation {
id
url
status
}
userErrors {
message
field
}
}
}';

$graphqlEndpoint = 'https://' . $GLOBALS['shopifyStore'] . '/admin/api/2024-01/graphql.json';
$accessToken = $GLOBALS['accessToken'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $graphqlEndpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Shopify-Access-Token: ' . $accessToken,
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['query' => $product_create_query]));
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
$graphqlResult = json_decode($response, true);
echo 'GraphQL Response: ' . PHP_EOL;
echo json_encode($graphqlResult) . PHP_EOL;
if (isset($graphqlResult['data']['bulkOperationRunMutation']['userErrors'])) {
foreach ($graphqlResult['data']['bulkOperationRunMutation']['userErrors'] as $userError) {
echo 'User Error: ' . $userError['message'] . PHP_EOL;
}
}
}
curl_close($ch);
}

// Function to perform stagedUploadsCreate GraphQL request
function stagedUploadsCreateData($requestData) {
$graphqlUrl = "https://$GLOBALS[shopifyStore]/admin/api/2024-01/graphql.json";
$headers = [
'Content-Type: application/json',
'X-Shopify-Access-Token: ' . $GLOBALS['accessToken'],
];
$ch = curl_init($graphqlUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}

error 
{
"data": {
"bulkOperationRunMutation": {
"bulkOperation": null,
"userErrors": [
{
"message": "Unexpected file structure - expected JSONL",
"field": null
}
]
}
},
"extensions": {
"cost": {
"requestedQueryCost": 10,
"actualQueryCost": 10,
"throttleStatus": {
"maximumAvailable": 2000,
"currentlyAvailable": 1990,
"restoreRate": 100
}
}
}
}

 
and i attached jsonl text file so please find out  

JAY P
Accepted Solution (1)

JayPRajput
Shopify Partner
5 1 0

This is an accepted solution.

I have resolved this issue Actually we need to update file url 

curl_file_create($localFilePath)
JAY P

View solution in original post

Reply 1 (1)

JayPRajput
Shopify Partner
5 1 0

This is an accepted solution.

I have resolved this issue Actually we need to update file url 

curl_file_create($localFilePath)
JAY P