0

Sorry, brand new to JSON, need some help.

I receive the following JSON array as a result from my API and need to get the very first string of numbers (= entity ID), which just so happens to not be a key or value. How can I get only that number in PHP while ignoring everything else?

Result:

"aa7a5d31-6114-47b5-a30c-27ad724cf212"{  // <-- this is what I need
   "statusCode":200,
   "effectiveUri":"https:\/\/workmail.eu-west-1.amazonaws.com",
   "headers":{
      "x-amzn-requestid":"long-number-code",
      "content-type":"application\/x-amz-json-1.1",
      "content-length":"49",
      "date":"Mon, 28 Jun 2021 13:46:18 GMT"
   },
   "transferStats":{
      "http":[
         [
            
         ]
      ]
   }
}

Thanks

7
  • That isn't valid JSON - try it at jsonlint.com Commented Jun 28, 2021 at 14:01
  • Sadly not much I can do about it, that's the data that comes back and I have to deal with -.- Commented Jun 28, 2021 at 14:01
  • 1
    Whoever did it like that needs to fix it - have given a bodge for an answer however Commented Jun 28, 2021 at 14:04
  • 3
    If you were told that you would get JSON, then the appropriate way of “dealing” with this is to reject it with an error message or exception or something, after the attempt to decode it as JSON failed. Commented Jun 28, 2021 at 14:04
  • New to JSON, but very experience in judging the guilty ;) Nope, this is the response I get from my API call, although I am sure there is a roundabout way to get that ID via 2 more calls, which is inefficient to say the least. Commented Jun 28, 2021 at 14:14

2 Answers 2

2
<?php
$json = '"aa7a5d31-6114-47b5-a30c-27ad724cf212"{
   "statusCode":200,
   "effectiveUri":"https:\/\/workmail.eu-west-1.amazonaws.com",
   "headers":{
      "x-amzn-requestid":"long-number-code",
      "content-type":"application\/x-amz-json-1.1",
      "content-length":"49",
      "date":"Mon, 28 Jun 2021 13:46:18 GMT"
   },
   "transferStats":{
      "http":[
         [
            
         ]
      ]
   }
}';

$result = str_replace('"','',explode("{",$json)[0]);
echo $result;
Sign up to request clarification or add additional context in comments.

Comments

0

Use a regular expression:

$json = '"aa7a5d31-6114-47b5-a30c-27ad724cf212"{
   "statusCode":200,
   "effectiveUri":"https:\/\/workmail.eu-west-1.amazonaws.com",
   "headers":{
      "x-amzn-requestid":"long-number-code",
      "content-type":"application\/x-amz-json-1.1",
      "content-length":"49",
      "date":"Mon, 28 Jun 2021 13:46:18 GMT"
   },
   "transferStats":{
      "http":[
         [
            
         ]
      ]
   }
}';

preg_match('/^"(.*?)"/', $json, $matches);
echo $matches[1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.