Jump to content

Decoding nested JSONs in PHP


montyonthebonty

Recommended Posts

I'm trying to create a PHP file to download some information which I then want to write to a MySQL Database. The raw information, after a gzdecode, looks a bit like this (there are many, many lines):

{"JsonAssociationV1":{"transaction_type":"Create","main_train_uid":"G31259","assoc_train_uid":"G32783","assoc_start_date":"2021-12-13T00:00:00Z","assoc_end_date":"2022-05-13T00:00:00Z","assoc_days":"1111100","category":"NP","date_indicator":"S","location":"SHEFFLD","base_location_suffix":null,"assoc_location_suffix":null,"diagram_type":"T","CIF_stp_indicator":"P"}}

{"JsonAssociationV1":{"transaction_type":"Create","main_train_uid":"G33347","assoc_train_uid":"G33111","assoc_start_date":"2021-12-13T00:00:00Z","assoc_end_date":"2022-05-13T00:00:00Z","assoc_days":"1111100","category":"NP","date_indicator":"S","location":"LEEDS","base_location_suffix":null,"assoc_location_suffix":null,"diagram_type":"T","CIF_stp_indicator":"P"}}

{"JsonAssociationV1":{"transaction_type":"Create","main_train_uid":"G31259","assoc_train_uid":"G34472","assoc_start_date":"2021-12-18T00:00:00Z","assoc_end_date":"2022-05-14T00:00:00Z","assoc_days":"0000010","category":"NP","date_indicator":"S","location":"SHEFFLD","base_location_suffix":null,"assoc_location_suffix":null,"diagram_type":"T","CIF_stp_indicator":"P"}}

After trying repeatedly and failing to read this directly, I decided to try converting it to an array, using a newline as a delimeter ($newArray = explode("\n", $json), which does work. To take one line as an example, this gives me an array for each line above. I've then done a json_decode on each line ($newLine = json_decode($your_array[1], true);), to produce this:

Array ( [JsonAssociationV1] => Array ( [transaction_type] => Create [main_train_uid] => G31259 [assoc_train_uid] => G32783 [assoc_start_date] => 2021-12-13T00:00:00Z [assoc_end_date] => 2022-05-13T00:00:00Z [assoc_days] => 1111100 [category] => NP [date_indicator] => S [location] => SHEFFLD [base_location_suffix] => [assoc_location_suffix] => [diagram_type] => T [CIF_stp_indicator] => P ) )

However, try as I might, I can't access the information in $newLine. What I want to is find out what type it is (in this case JsonAssociationV1) and then use the information in the nested array to write to the database.

Any help would be appreciated.

Thanks

Chris

Link to comment
Share on other sites

try

<?php
  $j = '{"JsonAssociationV1":{"transaction_type":"Create","main_train_uid":"G31259","assoc_train_uid":"G32783","assoc_start_date":"2021-12-13T00:00:00Z","assoc_end_date":"2022-05-13T00:00:00Z","assoc_days":"1111100","category":"NP","date_indicator":"S","location":"SHEFFLD","base_location_suffix":null,"assoc_location_suffix":null,"diagram_type":"T","CIF_stp_indicator":"P"}}';
  $a = json_decode($j, 1);
  $type = $a['JsonAssociationV1']['transaction_type'];
  
  echo $type;                                  // --> Create
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.