0

i have this json code:

    $dataB = <<<DATA
{


    "actualArrivalDateTime":"2021-09-07T10:00", 
    "containerNumber:" "000",
    "shipmentNumber": aaaa1, 
    "containerSequence":3,  
    "placeOfArrival": {
        "pointSequence": 2
                        }
                        
    
} 
DATA;

I want to pass php var to containerNumber but doesn't work . For example, if i assign:

<? $contnr = "001"; ?>

then i tried to do this:

    $dataB = <<<DATA
{


    "actualArrivalDateTime":"2021-09-07T10:00", 
    "containerNumber:" ".$var.",
    "shipmentNumber": aaaa1, 
    "containerSequence":3,  
    "placeOfArrival": {
        "pointSequence": 2
                        }
                        
    
} 
DATA;

but dowsn't work... it says that the containerNumber is not valid... but if i write the containerNumber, not as variable, it works. Any help?

  • 2
    Normally, you would convert your JSON string into a PHP array (or object) using `json_decode`, modify it, then convert it back to a JSON string using `json_encode`. – waterloomatt Sep 28 '21 at 13:15
  • can you pls helpme with an example? –  Sep 28 '21 at 13:17
  • `".$var."` should be `"$var"`: https://wandbox.org/permlink/h4dLNtbMyhbdrW17 Please do some debugging before you ask a question. A simple `echo $dataB` whould show you the problem. – jabaa Sep 28 '21 at 13:18
  • i tried to do as you said, and works when you write the json with echo $dataB. But when i pass this json to a server that receives these value it doesn't work.. –  Sep 28 '21 at 13:24

1 Answers1

2

Normally, you would convert your JSON string into a PHP array (or object) using json_decode, modify it, then convert it back to a JSON string using json_encode.

Your JSON string in the question is invalid. Use a tool such as this https://jsonlint.com/ to validate it.

Wrong placement of colon in "containerNumber:" and unquoted string aaaa1

{
    "actualArrivalDateTime":"2021-09-07T10:00", 
    "containerNumber:" "000",
    "shipmentNumber": aaaa1, 
    "containerSequence":3,  
    "placeOfArrival": {
        "pointSequence": 2
    }
}

It should be something like this,

{
    "actualArrivalDateTime": "2021-09-07T10:00",
    "containerNumber": "000",
    "shipmentNumber": "aaaa1",
    "containerSequence": 3,
    "placeOfArrival": {
        "pointSequence": 2
    }
}

http://sandbox.onlinephpfunctions.com/code/e8294e0b699bf560618dfa44efe9f09b892ecc31

<?php

$jsonString = '{
    "actualArrivalDateTime": "2021-09-07T10:00",
    "containerNumber": "000",
    "shipmentNumber": "aaaa1",
    "containerSequence": 3,
    "placeOfArrival": {
        "pointSequence": 2
    }
}';

// Convert it to an object
$jsonObject = json_decode($jsonString);

// Now change it
$jsonObject->containerNumber = '001';

// Convert it back to a JSON string. 
echo json_encode($jsonObject);
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • hi, thanks for your help. Do you know how can i check if the the server response is 200 or not? Thanks for your help –  Sep 28 '21 at 15:21
  • What are you using to make the request? AJAX, CURL, Guzzle? Might be better to ask a new question. – waterloomatt Sep 28 '21 at 15:43
  • i used curl: curl_setopt($curlB, CURLOPT_POSTFIELDS, $dataB); //for debug only! curl_setopt($curlB, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curlB, CURLOPT_SSL_VERIFYPEER, false); –  Sep 28 '21 at 15:50
  • can i contact you via msgs? I can explain better... here i cannot post a new question or answer, i only can write as comment :( –  Sep 28 '21 at 15:51
  • Sorry, don't have time right now, but I'd start here - https://stackoverflow.com/q/11797680/296555. Also, don't forget to mark an answer as "accepted" if it helped you out. Thanks. – waterloomatt Sep 28 '21 at 15:57