0

I am trying to decode JSON

I am reading the JSON through STOMP. There are different JSON datasets so I need to work out which JSON dataset has come through. I do this by reading its title.

However there is one particular dataset I am having trouble reading

  foreach (json_decode($msg->body,true) as $event) {
    if(isset($event['schemaLocation'])) {
    $schedule_id=($event['schedule']['schedule_start_date']); 
    $signalling_id=($event['schedule']['schedule_segment']['signalling_id']);

    echo $schedule_id;
    }

In the above example the isset function works fine and also $schedule_id obtains the right answer

However the $signalling_id gives an error of Undefined index:

Here is a dump of PART of the JSON (Its rather long............).The piece of JSON with the signalling_id is towards the end of the JSON. Any help to get the variable signalling_id much appreciated.

    array(7) {
    ["schemaLocation"]=>
    string(72) "http://xml.networkrail.co.uk/ns/2008/Train itm_vstp_cif_messaging_v1.xsd"
    ["classification"]=>
    string(8) "industry"
    ["timestamp"]=>
    string(13) "1410374918000"
    ["owner"]=>
    string(12) "Network Rail"
    ["originMsgId"]=>
    string(47) "2014-09-10T18:48:38-00:00vstp.networkrail.co.uk"
    ["Sender"]=>
    array(3) {
      ["organisation"]=>
      string(12) "Network Rail"
      ["application"]=>
      string(4) "TOPS"
      ["component"]=>
      string(4) "VSTP"
    }
    ["schedule"]=>
    array(11) {
      ["schedule_id"]=>
      string(0) ""
      ["transaction_type"]=>
      string(6) "Create"
      ["schedule_start_date"]=>
      string(10) "2014-09-10"
      ["schedule_end_date"]=>
      string(10) "2014-09-10"
      ["schedule_days_runs"]=>
      string(7) "0010000"
      ["applicable_timetable"]=>
      string(1) "N"
      ["CIF_bank_holiday_running"]=>
      string(1) " "
      ["CIF_train_uid"]=>
      string(6) "W64017"
      ["train_status"]=>
      string(1) "1"
      ["CIF_stp_indicator"]=>
      string(1) "O"
      ["schedule_segment"]=>
      array(1) {
        [0]=>
        array(20) {
          ["signalling_id"]=>
          string(4) "5Y75"
          ["uic_code"]=>
          string(0) ""
          ["atoc_code"]=>
          string(0) ""
          ["CIF_train_category"]=>
          string(2) "EE"
          ["CIF_headcode"]=>
          string(0) ""
          ["CIF_course_indicator"]=
          ............................................ 

2 Answers 2

1

schedule_segment is itself an array, so instead of

['schedule']['schedule_segment']['signalling_id']);

that should probably be

['schedule']['schedule_segment'][0]['signalling_id']);
Sign up to request clarification or add additional context in comments.

Comments

1

As you can see in the var dump, signalling_id is inside another array. Use:

$signalling_id=($event ['schedule']['schedule_segment'][0]['signalling_id']);

If that one element array with key 0 is not constant throughout, you may need some logic to figure out what it is in each iteration.

1 Comment

Just testing that now thanks. Waiting for JSON to come through STOMP

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.