0

I want to convert XML response to JSON data, but the response is more complex than I want.

My PHP file:

...
    $client = new SoapClient($wsdl_path, array('login' => $login, 'password' => $password));

    $params = array('ProductId' => "C0041000");
    $result = $client->__soapCall('GetProduct', array('parameters' => $params));        
    $xml = simplexml_load_string($result->GetProductResult->any);

    echo json_encode($xml);
    var_dump($xml);

My response in browser:

{"DocumentInfo":{"DocType":"PRODUCT","DocVersion":"2.3"},"Header":{"LangId":"RO"},"Products":{"Product":{"ProductId":"C0041000","PartNumber":"KS-MC-CC12","ClientProductId":{},"ProductName":{},"MajorGroup":"MOD","MinorGroup":"VDC","HierarchyId":"36491","ProducerId":"MODE_COM","Price":"413.17","CurrencyId":"RON","Available":"+","RecyclingFee":"0.84","CopyrightFee":"0","VatRate":"0.24","WarrantyId":"A ","AttribGroupId":"481","BarCodes":{"BarCodeInfo":{"BarCodeType":"EAN13","BarCode":"5901885242651"}},"VatCode":"24%","AvailableExternal":"-"}}}

object(SimpleXMLElement)[4]
  public 'DocumentInfo' => 
    object(SimpleXMLElement)[7]
      public 'DocType' => string 'PRODUCT' (length=7)
      public 'DocVersion' => string '2.3' (length=3)
  public 'Header' => 
    object(SimpleXMLElement)[8]
      public 'LangId' => string 'RO' (length=2)
  public 'Products' => 
    object(SimpleXMLElement)[9]
      public 'Product' => 
        object(SimpleXMLElement)[12]
          public 'ProductId' => string 'C0041000' (length=8)
          public 'PartNumber' => string 'KS-MC-CC12' (length=10)
          public 'ClientProductId' => 
            object(SimpleXMLElement)[10]
              ...
          public 'ProductName' => 
            object(SimpleXMLElement)[11]
              ...
          public 'MajorGroup' => string 'MOD' (length=3)
          public 'MinorGroup' => string 'VDC' (length=3)
          public 'HierarchyId' => string '36491' (length=5)
          public 'ProducerId' => string 'MODE_COM' (length=8)
          public 'Price' => string '413.17' (length=6)
          public 'CurrencyId' => string 'RON' (length=3)
          public 'Available' => string '+' (length=1)
          public 'RecyclingFee' => string '0.84' (length=4)
          public 'CopyrightFee' => string '0' (length=1)
          public 'VatRate' => string '0.24' (length=4)
          public 'WarrantyId' => string 'A   ' (length=4)
          public 'AttribGroupId' => string '481' (length=3)
          public 'BarCodes' => 
            object(SimpleXMLElement)[6]
              ...
          public 'VatCode' => string '24%' (length=3)
          public 'AvailableExternal' => string '-' (length=1)

I need from this json only data from Product area like ProductId and PriceId fields, but I am not able to do that.

I was trying with:

json_encode($xml->Products->Product->Price);

but is not working.

Thanks in advance for any solution!

1 Answer 1

1

You can create an array and cast it as an object, setting only the properties you need:

echo json_encode((object) array('productId' => $xml->Products->Product->ProductId, 'price' => $xml->Products->Product->Price));
Sign up to request clarification or add additional context in comments.

2 Comments

OK. It's working! Now in browser I have {"productId":{"0":"C0041000"},"price":{"0":"413.17"}}. If I want to echo only the value of Price field?
just that single value? just echo it :) echo $xml->Products->Product->Price;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.