montyonthebonty Posted January 10, 2022 Share Posted January 10, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314397-decoding-nested-jsons-in-php/ Share on other sites More sharing options...
Barand Posted January 10, 2022 Share Posted January 10, 2022 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/314397-decoding-nested-jsons-in-php/#findComment-1593262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.