0

Whhat I am trying to achieve is to flatten a JSON nested response directly on the fly. The response I am getting from the API URL is

 {   "currency" : "USD",   "results" : [ {
 "itineraries" : [ {
   "outbound" : {
     "duration" : "17:50",
     "flights" : [ {
       "departs_at" : "2018-10-15T08:45",
       "arrives_at" : "2018-10-15T11:00",
       "origin" : {
         "airport" : "IST",
         "terminal" : "I"
       },
       "destination" : {
        "airport" : "LHR",
        "terminal" : "5"
      },
      "marketing_airline" : "BA",
      "operating_airline" : "BA",
      "flight_number" : "675",
      "aircraft" : "320",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "S",
        "seats_remaining" : 7
      }
    }, {
      "departs_at" : "2018-10-15T16:55",
      "arrives_at" : "2018-10-15T19:35",
      "origin" : {
        "airport" : "LHR",
        "terminal" : "5"
      },
      "destination" : {
        "airport" : "BOS",
        "terminal" : "E"
      },
      "marketing_airline" : "BA",
      "operating_airline" : "BA",
      "flight_number" : "203",
      "aircraft" : "777",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "N",
        "seats_remaining" : 9
      }
    } ]
  },
  "inbound" : {
    "duration" : "14:00",
    "flights" : [ {
      "departs_at" : "2018-10-21T19:25",
      "arrives_at" : "2018-10-22T07:00",
      "origin" : {
        "airport" : "BOS",
        "terminal" : "E"
      },
      "destination" : {
        "airport" : "LHR",
        "terminal" : "5"
      },
      "marketing_airline" : "BA",
      "operating_airline" : "BA",
      "flight_number" : "212",
      "aircraft" : "388",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "N",
        "seats_remaining" : 9
      }
    }, {
      "departs_at" : "2018-10-22T10:25",
      "arrives_at" : "2018-10-22T16:25",
      "origin" : {
        "airport" : "LHR",
        "terminal" : "5"
      },
      "destination" : {
        "airport" : "IST",
        "terminal" : "I"
      },
      "marketing_airline" : "BA",
      "operating_airline" : "BA",
      "flight_number" : "676",
      "aircraft" : "320",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "S",
        "seats_remaining" : 9
      }
    } ]
  }
} ],
"fare" : {
  "total_price" : "486.17",
  "price_per_adult" : {
    "total_fare" : "486.17",
    "tax" : "343.17"
  },
  "restrictions" : {
    "refundable" : false,
    "change_penalties" : true
  }
}   }, {
"itineraries" : [ {
  "outbound" : {
    "duration" : "10:55",
    "flights" : [ {
      "departs_at" : "2018-10-15T14:35",
      "arrives_at" : "2018-10-15T18:30",
      "origin" : {
        "airport" : "IST",
        "terminal" : "I"
      },
      "destination" : {
        "airport" : "BOS",
        "terminal" : "E"
      },
      "marketing_airline" : "TK",
      "operating_airline" : "TK",
      "flight_number" : "81",
      "aircraft" : "333",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "W",
        "seats_remaining" : 9
      }
    } ]
  },
  "inbound" : {
    "duration" : "09:35",
    "flights" : [ {
      "departs_at" : "2018-10-21T23:35",
      "arrives_at" : "2018-10-22T16:10",
      "origin" : {
        "airport" : "BOS",
        "terminal" : "E"
      },
      "destination" : {
        "airport" : "IST",
        "terminal" : "I"
      },
      "marketing_airline" : "TK",
      "operating_airline" : "TK",
      "flight_number" : "82",
      "aircraft" : "333",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "W",
        "seats_remaining" : 9
      }
    } ]
  }
} ],
"fare" : {
  "total_price" : "528.31",
  "price_per_adult" : {
    "total_fare" : "528.31",
    "tax" : "344.31"
  },
  "restrictions" : {
    "refundable" : false,
    "change_penalties" : true
  }
}   } ] }

And what I basically need is to get it formatted in a flat way, so everything is on the same level.

I am getting the response file_get_content like this $json_url = file_get_contents('https ://domain/api'); And if I print it it works fine, but having troubles simplifying the result to a flat JSON Response.

EDIT Something like

 {
    "currency": "USD",
    "results[0].itineraries[0].outbound.duration": "17:50",
    "results[0].itineraries[0].outbound.flights[0].departs_at": "2018-10-15T08:45",
    "results[0].itineraries[0].outbound.flights[0].arrives_at": "2018-10-15T11:00",
    "results[0].itineraries[0].outbound.flights[0].origin.airport": "IST",
    "results[0].itineraries[0].outbound.flights[0].origin.terminal": "I",
    "results[0].itineraries[0].outbound.flights[0].destination.airport": "LHR",
    "results[0].itineraries[0].outbound.flights[0].destination.terminal": "5",
    "results[0].itineraries[0].outbound.flights[0].marketing_airline": "BA",
    "results[0].itineraries[0].outbound.flights[0].operating_airline": "BA",
    "results[0].itineraries[0].outbound.flights[0].flight_number": "675",
    "results[0].itineraries[0].outbound.flights[0].aircraft": "320",
    "results[0].itineraries[0].outbound.flights[0].booking_info.travel_class": "ECONOMY",
    "results[0].itineraries[0].outbound.flights[0].booking_info.booking_code": "S",
    "results[0].itineraries[0].outbound.flights[0].booking_info.seats_remaining": 7,
    "results[0].itineraries[0].outbound.flights[1].departs_at": "2018-10-15T16:55",
    "results[0].itineraries[0].outbound.flights[1].arrives_at": "2018-10-15T19:35",
    "results[0].itineraries[0].outbound.flights[1].origin.airport": "LHR",
    "results[0].itineraries[0].outbound.flights[1].origin.terminal": "5",
    "results[0].itineraries[0].outbound.flights[1].destination.airport": "BOS",
    "results[0].itineraries[0].outbound.flights[1].destination.terminal": "E",
    "results[0].itineraries[0].outbound.flights[1].marketing_airline": "BA",
    "results[0].itineraries[0].outbound.flights[1].operating_airline": "BA",
    "results[0].itineraries[0].outbound.flights[1].flight_number": "203",
    "results[0].itineraries[0].outbound.flights[1].aircraft": "777",
    "results[0].itineraries[0].outbound.flights[1].booking_info.travel_class": "ECONOMY",
    "results[0].itineraries[0].outbound.flights[1].booking_info.booking_code": "N",
    "results[0].itineraries[0].outbound.flights[1].booking_info.seats_remaining": 9,
    "results[0].itineraries[0].inbound.duration": "14:00",
    "results[0].itineraries[0].inbound.flights[0].departs_at": "2018-10-21T19:25",
    "results[0].itineraries[0].inbound.flights[0].arrives_at": "2018-10-22T07:00",
    "results[0].itineraries[0].inbound.flights[0].origin.airport": "BOS",
    "results[0].itineraries[0].inbound.flights[0].origin.terminal": "E",
    "results[0].itineraries[0].inbound.flights[0].destination.airport": "LHR",
    "results[0].itineraries[0].inbound.flights[0].destination.terminal": "5",
    "results[0].itineraries[0].inbound.flights[0].marketing_airline": "BA",
    "results[0].itineraries[0].inbound.flights[0].operating_airline": "BA",
    "results[0].itineraries[0].inbound.flights[0].flight_number": "212",
    "results[0].itineraries[0].inbound.flights[0].aircraft": "388",
    "results[0].itineraries[0].inbound.flights[0].booking_info.travel_class": "ECONOMY",
    "results[0].itineraries[0].inbound.flights[0].booking_info.booking_code": "N",
    "results[0].itineraries[0].inbound.flights[0].booking_info.seats_remaining": 9,
    "results[0].itineraries[0].inbound.flights[1].departs_at": "2018-10-22T10:25",
    "results[0].itineraries[0].inbound.flights[1].arrives_at": "2018-10-22T16:25",
    "results[0].itineraries[0].inbound.flights[1].origin.airport": "LHR",
    "results[0].itineraries[0].inbound.flights[1].origin.terminal": "5",
    "results[0].itineraries[0].inbound.flights[1].destination.airport": "IST",
    "results[0].itineraries[0].inbound.flights[1].destination.terminal": "I",
    "results[0].itineraries[0].inbound.flights[1].marketing_airline": "BA",
    "results[0].itineraries[0].inbound.flights[1].operating_airline": "BA",
    "results[0].itineraries[0].inbound.flights[1].flight_number": "676",
    "results[0].itineraries[0].inbound.flights[1].aircraft": "320",
    "results[0].itineraries[0].inbound.flights[1].booking_info.travel_class": "ECONOMY",
    "results[0].itineraries[0].inbound.flights[1].booking_info.booking_code": "S",
    "results[0].itineraries[0].inbound.flights[1].booking_info.seats_remaining": 9,
    "results[0].fare.total_price": "486.17",
    "results[0].fare.price_per_adult.total_fare": "486.17",
    "results[0].fare.price_per_adult.tax": "343.17",
    "results[0].fare.restrictions.refundable": false,
    "results[0].fare.restrictions.change_penalties": true
    }
    {
    "currency": "USD",
    "results[1].itineraries[0].outbound.duration": "10:55",
    "results[1].itineraries[0].outbound.flights[0].departs_at": "2018-10-15T14:35",
    "results[1].itineraries[0].outbound.flights[0].arrives_at": "2018-10-15T18:30",
    "results[1].itineraries[0].outbound.flights[0].origin.airport": "IST",
    "results[1].itineraries[0].outbound.flights[0].origin.terminal": "I",
    "results[1].itineraries[0].outbound.flights[0].destination.airport": "BOS",
    "results[1].itineraries[0].outbound.flights[0].destination.terminal": "E",
    "results[1].itineraries[0].outbound.flights[0].marketing_airline": "TK",
    "results[1].itineraries[0].outbound.flights[0].operating_airline": "TK",
    "results[1].itineraries[0].outbound.flights[0].flight_number": "81",
    "results[1].itineraries[0].outbound.flights[0].aircraft": "333",
    "results[1].itineraries[0].outbound.flights[0].booking_info.travel_class": "ECONOMY",
    "results[1].itineraries[0].outbound.flights[0].booking_info.booking_code": "W",
    "results[1].itineraries[0].outbound.flights[0].booking_info.seats_remaining": 9,
    "results[1].itineraries[0].inbound.duration": "09:35",
    "results[1].itineraries[0].inbound.flights[0].departs_at": "2018-10-21T23:35",
    "results[1].itineraries[0].inbound.flights[0].arrives_at": "2018-10-22T16:10",
    "results[1].itineraries[0].inbound.flights[0].origin.airport": "BOS",
    "results[1].itineraries[0].inbound.flights[0].origin.terminal": "E",
    "results[1].itineraries[0].inbound.flights[0].destination.airport": "IST",
    "results[1].itineraries[0].inbound.flights[0].destination.terminal": "I",
    "results[1].itineraries[0].inbound.flights[0].marketing_airline": "TK",
    "results[1].itineraries[0].inbound.flights[0].operating_airline": "TK",
    "results[1].itineraries[0].inbound.flights[0].flight_number": "82",
    "results[1].itineraries[0].inbound.flights[0].aircraft": "333",
    "results[1].itineraries[0].inbound.flights[0].booking_info.travel_class": "ECONOMY",
    "results[1].itineraries[0].inbound.flights[0].booking_info.booking_code": "W",
    "results[1].itineraries[0].inbound.flights[0].booking_info.seats_remaining": 9,
    "results[1].fare.total_price": "528.31",
    "results[1].fare.price_per_adult.total_fare": "528.31",
    "results[1].fare.price_per_adult.tax": "344.31",
    "results[1].fare.restrictions.refundable": false,
    "results[1].fare.restrictions.change_penalties": true
}

Thanks

15
  • That is impossible, if you flatten the Json/array how are you going to access for example "departs_at"? And which "departs_at" is it going to respond back with? Commented Aug 4, 2018 at 20:37
  • Do you want to convert it to single dimensional array? Commented Aug 4, 2018 at 20:38
  • @Andreas yes, that's, duplicate keys will be overwritten. Commented Aug 4, 2018 at 20:40
  • 1
    I don't understand what or why you are trying to do it. Your original question can't be answered so either the question should be closed of you should edit it to a state that it can be answered Commented Aug 4, 2018 at 20:55
  • 1
    @Andreas OP want to concat keys upto value as key then assign this value to long key. results[0].itineraries[0].outbound.duration as key, 17:50 value. Commented Aug 4, 2018 at 21:27

1 Answer 1

1

Here is a method that replicates your expected json format.
I have borrowed some code from This answer, and tweeked slightly to make it work for you, remember to give it an upvote.

I use the code from my comments to create the currency part of the array.
Then I pass each of the new subarray part to the code that backtracks the path and makes it json code.

$arr = json_decode($str,true);  // $str is the json
foreach($arr['results'] as $key => $res){
    $new[$key]['currency'] = $arr['currency'];
    $new[$key]['results'][] = $res;
}
foreach($new as $n){
    $json[] = listArrayRecursive($n);
}
$jsonNew = "[" . implode(",", $json) . "]"; // this is the new json formed as you want it
var_dump(json_decode($jsonNew), true); // for debug purpose test it




function listArrayRecursive($someArray) {
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($someArray), RecursiveIteratorIterator::SELF_FIRST);
    $str = "{";
    foreach ($iterator as $k => $v) {

        // Not at end: show key only
        if ($iterator->hasChildren()) {

        } else {
            for ($p = array(), $i = 0, $z = $iterator->getDepth(); $i <= $z; $i++) {
                $p[] = $iterator->getSubIterator($i)->key();
            }
            $path = implode('.', $p);
            $str .= '"' . $path . '": "' .$v . '"' . ",";
        }
    }
    $str = substr($str,0,-1); // remove trailing comma that makes json invalid
    $str .= "}\n";
    return $str;
}

https://3v4l.org/eRCIY

Sign up to request clarification or add additional context in comments.

7 Comments

That looks already great. Just one thing. Is there a way to remove the array(2) { [0]=> object(stdClass)#1 (60) and start directly from { ["currency"]=> string(3) "USD"
I mean the output to be like [ {}, {} ]
Isn't that what it is? Or are you again asking for a flat array where there is keys with the same name? It will not work! Trust me.
If you look at this: 3v4l.org/9iGZD each $json element is { } and they are imploded with a comma. { },{ } then the concatenat makes it [{ },{ }]
Why do you Json encode there? What is the point? And obviously there is no comma between the arrays since the code implodes with comma later. Adding a comma in the array just makes it harder.
|

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.