0

I have been trying to decode a JSON response from a third party server (Payment gateway). The issue I can not decode the JSON because one of the JSON key's value is multi-line.

What I have done so far:-

$jsonobj = '{
    "requestId":"123",
    "errorCode": "322231231",
    "errorMessage": "lorem ipsum1
            lorem ipsum2
            lorem ipsum3
        "
}';

print_r(json_decode($jsonobj, true));

As the JSON response comes from a third party how to handle this situation?

MjM
  • 571
  • 4
  • 15
  • 1
    [Are multi-line strings allowed in JSON?](https://stackoverflow.com/questions/2392766/are-multi-line-strings-allowed-in-json) – Definitely not Rafal Jan 14 '21 at 11:54
  • 1
    If this is what you got and now you need to fix it on your end - then you will have to pre-parse it yourself, and modify it into valid JSON. You could try to approach this with regular expressions, but depending on what actual data you will be getting, this could easily get quite complex and error-prone. – CBroe Jan 14 '21 at 11:56
  • 1
    https://3v4l.org/Gtn8E . json data needs to be with `\n` over there – Alive to die - Anant Jan 14 '21 at 12:03
  • @CBroe, The JSON response is coming from a payment gateway when payment getting failed. As it coming from the payment gateway, do you believe parsing the response is safe? actually, the response is not standard one right? – MjM Jan 14 '21 at 12:11
  • 1
    Could a payment gateway that does not even get trivial stuff like this right, ever be considered trustworthy in _any_ way …? I’d file a bug report, and ask them to fix this. If they refuse, they are not a partner to work with. – CBroe Jan 14 '21 at 12:12

1 Answers1

-1

Try this code, it can search key:

<?php
$jsonobj = '{ "requestId":"123", "errorCode": "322231231", "errorMessage": "lorem ipsum1 lorem ipsum2 lorem ipsum3 " }';
$decode = json_decode($jsonobj, true);
foreach($decode as $key){
     echo 'value: "'.$key.'" have key: "'.array_search($decode,$key).'"';
}
?>

by: https://www.php.net/manual/en/function.array-search.php