parsing JSON data with PHP....large dataset - php
below is the dataset I am trying to parse and put into db... I am using this code
$json = json_decode($string);
$json[0]['segments']['layover'] gives access error...
also sizeof($json) returns nothing...
$json doesn't return null,is checked, prints with echo var_dump($json)
I have attempted to foreach through this...doesn't work.. tried an array type and is saying that I am illegal access stdIN object...is there anything as easy as DOMDocument() to walk through json?
---json
[{"legs":[{"segments":[{"layover":{"hours":1,"minutes":28},"totalTravelingTime":{"hours":1,"minutes":10},"departureAirport":{"airportCode":"ABE","airportCity":"Allentown","airportName":"Allentown (ABE)","airportCityState":"Allentown, PA"},"arrivalAirport":{"airportCode":"IAD","airportCity":"Washington","airportName":"Washington (IAD)","airportCityState":"Washington, DC"},"departureDate":1328811900000,"arrivalDate":1328816100000,"flightNumber":"3866","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":25,"hour":10,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T10:25:00-05:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"UA","aircraftType":"Saab 340/340B","aircraftCode":"SF3","airline":{"activeState":"ACTIVE","airlineCode":"UA","airlineName":"UNITED","majorAirline":true,"imageFilename":"UA.gif","preferredAirline":false},"bookingCode":"S","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":1,"formattedDepartureTime":"10:25am","formattedArrivalTime":"11:35am","operatedBy":"/UNITED EXPRESS/COLGAN AIR","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"","wiFiAvailable":false},{"layover":{"hours":1,"minutes":5},"totalTravelingTime":{"hours":3,"minutes":27},"departureAirport":{"airportCode":"IAD","airportCity":"Washington","airportName":"Washington (IAD)","airportCityState":"Washington, DC"},"arrivalAirport":{"airportCode":"DFW","airportCity":"Dallas","airportName":"Dallas (Dallas-Fort Worth Intl.)","airportCityState":"Dallas, TX"},"departureDate":1328821380000,"arrivalDate":1328830200000,"flightNumber":"3801","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":3,"hour":13,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T13:03:00-05:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"UA","aircraftType":"Canadian Regional Jet 700","aircraftCode":"CR7","airline":{"activeState":"ACTIVE","airlineCode":"UA","airlineName":"UNITED","majorAirline":true,"imageFilename":"UA.gif","preferredAirline":false},"bookingCode":"S","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":2,"formattedDepartureTime":"1:03pm","formattedArrivalTime":"3:30pm","operatedBy":"/UNITED EXPRESS/MESA AIRLINES","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"","wiFiAvailable":false},{"totalTravelingTime":{"hours":0,"minutes":50},"departureAirport":{"airportCode":"DFW","airportCity":"Dallas","airportName":"Dallas (Dallas-Fort Worth Intl.)","airportCityState":"Dallas, TX"},"arrivalAirport":{"airportCode":"ABI","airportCity":"Abilene","airportName":"Abilene (ABI)","airportCityState":"Abilene, TX"},"departureDate":1328834100000,"arrivalDate":1328837100000,"flightNumber":"2975","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-06:00","day":9,"second":0,"minute":35,"hour":16,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T16:35:00-06:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"AA","aircraftType":"Embraer RJ145","aircraftCode":"ER4","airline":{"activeState":"ACTIVE","airlineCode":"AA","airlineName":"American Airlines","majorAirline":true,"imageFilename":"AA.gif","preferredAirline":false},"bookingCode":"L","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":3,"formattedDepartureTime":"4:35pm","formattedArrivalTime":"5:25pm","operatedBy":"/AMERICAN EAGLE","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"","wiFiAvailable":false}],"carriers":["UA","UA","AA"],"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":25,"hour":10,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T10:25:00-05:00"},"arrivalDateTime":{"month":2,"year":2012,"timeZone":"GMT-06:00","day":9,"second":0,"minute":25,"hour":17,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T17:25:00-06:00"},"airLineImageFilePath":"","elapsedDays":0,"departureTime":"2012/02/09 10:25:00 PST","arrivalTime":"2012/02/09 17:25:00 PST","departureDate":1328811900000,"arrivalDate":1328837100000,"arrivalAirport":{"airportCode":"ABI","airportCity":"Abilene","airportName":"Abilene (ABI)","airportCityState":"Abilene, TX"},"legSequenceIndex":1,"departureAirport":{"airportCode":"ABE","airportCity":"Allentown","airportName":"Allentown (ABE)","airportCityState":"Allentown, PA"},"departureDateFormat":"2012/02/09","arrivalDateFormat":"Feb. 9","formattedDepartureTime":"10:25am","formattedArrivalTime":"5:25pm","hasTrainLeg":false,"stops":2,"totalTravelingHours":{"hours":8,"minutes":0},"formattedMiles":"1,495","hasBusLeg":false,"formattedDepartureDate":"2/9/12","formattedDepartureDateTime":"2/9/12","departureDateFormat1":"Thu. Feb. 9","formattedArrivalDate":"2/9/12","durationTimeInMins":480,"locationDistanceCount":1495,"locationDistanceUnit":"miles","seatMapAvailableForAnySegment":true,"firstBagFreeAirlines":false,"showOBFeeMessageForLeg":false,"airlineWithBagFeeMsg":false,"airlineWithNoBagFeeMsg":false,"legIndex":1}],"inboundLegsInfo":[{"taxes":43.22,"basePrice":808.01,"taxesAndFees":50.22,"ceilTotalPrice":859.0,"localeFormattedTaxAndFees":"$50","localeFormatBasePrice":"$808","bookingFee":7.0,"t":858.23,"ft":"$858","fab":"$808","fat":"$858","faf":"$50","tripId":30,"s":2,"c":"AA"},{"taxes":43.22,"basePrice":1028.01,"taxesAndFees":50.22,"ceilTotalPrice":1079.0,"localeFormattedTaxAndFees":"$50","localeFormatBasePrice":"$1,028","bookingFee":7.0,"t":1078.23,"ft":"$1,078","fab":"$1,028","fat":"$1,078","faf":"$50","tripId":50,"s":2,"c":"UA"}],"airFareBasisCode":"SA0MN","flightFareType":"PUBLISHED","seatCount":1,"totalPrice":858.23,"fare":{"maxLccPrice":0.0,"minLccPrice":0.0,"totalFlightFare":858.23,"currencyCode":"USD","fees":{"amount":7.00,"currency":"USD","double":7.0,"currencyCode":"USD"},"taxes":{"amount":43.22,"currency":"USD","double":43.22,"currencyCode":"USD"},"baseFare":{"amount":808.01,"currency":"USD","double":808.01,"currencyCode":"USD"},"totalFare":{"amount":858.23,"currency":"USD","double":858.23,"currencyCode":"USD"},"totalBaseFare":808.01,"localeTotalBaseFare":"$808","localeTotalFlightTaxes":"$43","totalFlightTaxes":43.22,"localeTotalFlightFare":"$858","localeTotalFees":"$7","localeTotalFlightTaxesAndFees":"$50","totalFees":7.0,"totalTaxesAndFees":{"amount":50.22,"currency":"USD","double":50.22,"currencyCode":"USD"},"flightFare":{"currencyCode":"USD","basePrice":808.01,"taxes":43.22}},"flightId":"30","showFareDisplayName":false,"fareDisplayName":"Published","firstBagFreeAirline":false,"operatedByFeeDisplayFeatureEnabled":false,"hasFare":true,"localeFormatAvgBasePrice":"$808","localeFormatAvgTotalPrice":"$858","stops":2,"bargainFare":false,"fareName":"published","selectedFlightPara":"30:30:FLTIStr","fareTypeCode":"P","airProviderId":2,"ccfeeAirline":"","displayLCCFareName":false,"noOfTicketsLeft":7,"seatPreview":true,"localeFormatAvgTotalTaxesAndFees":"$50","priceIllegal":false,"fareTypeValue":"1","showOBFeeMessageForModule":false,"completeTrip":false,"hasAirlineWithBagFee":false,"hasAirlineWithNoBagFee":false,"isABestTrip":true,"carrierList":["AA"],"isFirstBagFreePos":true},{"legs":[{"segments":[{"layover":{"hours":1,"minutes":41},"totalTravelingTime":{"hours":2,"minutes":22},"departureAirport":{"airportCode":"ABE","airportCity":"Allentown","airportName":"Allentown (ABE)","airportCityState":"Allentown, PA"},"arrivalAirport":{"airportCode":"ATL","airportCity":"Atlanta","airportName":"Atlanta (Hartsfield-Jackson Atlanta Intl.)","airportCityState":"Atlanta, GA"},"departureDate":1328818020000,"arrivalDate":1328826540000,"flightNumber":"4355","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":7,"hour":12,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T12:07:00-05:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"DL","aircraftType":"Canadair RJ","aircraftCode":"CRJ","airline":{"activeState":"ACTIVE","airlineCode":"DL","airlineName":"Delta","majorAirline":true,"imageFilename":"DL.gif","preferredAirline":false},"bookingCode":"Q","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":1,"formattedDepartureTime":"12:07pm","formattedArrivalTime":"2:29pm","operatedBy":"/PINNACLE DBA DELTA CONNECTION","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"9E","wiFiAvailable":false},{"layover":{"hours":1,"minutes":5},"totalTravelingTime":{"hours":2,"minutes":30},"departureAirport":{"airportCode":"ATL","airportCity":"Atlanta","airportName":"Atlanta (Hartsfield-Jackson Atlanta Intl.)","airportCityState":"Atlanta, GA"},"arrivalAirport":{"airportCode":"DFW","airportCity":"Dallas","airportName":"Dallas (Dallas-Fort Worth Intl.)","airportCityState":"Dallas, TX"},"departureDate":1328832600000,"arrivalDate":1328838000000,"flightNumber":"1810","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":10,"hour":16,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T16:10:00-05:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"DL","aircraftType":"Boeing 757-200","aircraftCode":"752","airline":{"activeState":"ACTIVE","airlineCode":"DL","airlineName":"Delta","majorAirline":true,"imageFilename":"DL.gif","preferredAirline":false},"bookingCode":"Q","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":2,"formattedDepartureTime":"4:10pm","formattedArrivalTime":"5:40pm","operatedBy":"","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"","wiFiAvailable":false},{"totalTravelingTime":{"hours":0,"minutes":50},"departureAirport":{"airportCode":"DFW","airportCity":"Dallas","airportName":"Dallas (Dallas-Fort Worth Intl.)","airportCityState":"Dallas, TX"},"arrivalAirport":{"airportCode":"ABI","airportCity":"Abilene","airportName":"Abilene (ABI)","airportCityState":"Abilene, TX"},"departureDate":1328841900000,"arrivalDate":1328844900000,"flightNumber":"2773","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-06:00","day":9,"second":0,"minute":45,"hour":18,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T18:45:00-06:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"AA","aircraftType":"Embraer EMB-140","aircraftCode":"ERD","airline":{"activeState":"ACTIVE","airlineCode":"AA","airlineName":"American Airlines","majorAirline":true,"imageFilename":"AA.gif","preferredAirline":false},"bookingCode":"L","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":3,"formattedDepartureTime":"6:45pm","formattedArrivalTime":"7:35pm","operatedBy":"/AMERICAN EAGLE","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"","wiFiAvailable":false}],"carriers":["DL","DL","AA"],"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":7,"hour":12,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T12:07:00-05:00"},"arrivalDateTime":{"month":2,"year":2012,"timeZone":"GMT-06:00","day":9,"second":0,"minute":35,"hour":19,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T19:35:00-06:00"},"airLineImageFilePath":"","elapsedDays":0,"departureTime":"2012/02/09 12:07:00 PST","arrivalTime":"2012/02/09 19:35:00 PST","departureDate":1328818020000,"arrivalDate":1328844900000,"arrivalAirport":{"airportCode":"ABI","airportCity":"Abilene","airportName":"Abilene (ABI)","airportCityState":"Abilene, TX"},"legSequenceIndex":10,"departureAirport":{"airportCode":"ABE","airportCity":"Allentown","airportName":"Allentown (ABE)","airportCityState":"Allentown, PA"},"departureDateFormat":"2012/02/09","arrivalDateFormat":"Feb. 9","formattedDepartureTime":"12:07pm","formattedArrivalTime":"7:35pm","hasTrainLeg":false,"stops":2,"totalTravelingHours":{"hours":8,"minutes":28},"formattedMiles":"1,581","hasBusLeg":false,"formattedDepartureDate":"2/9/12","formattedDepartureDateTime":"2/9/12","departureDateFormat1":"Thu. Feb. 9","formattedArrivalDate":"2/9/12","durationTimeInMins":508,"locationDistanceCount":1581,"locationDistanceUnit":"miles","seatMapAvailableForAnySegment":true,"firstBagFreeAirlines":false,"showOBFeeMessageForLeg":false,"airlineWithBagFeeMsg":false,"airlineWithNoBagFeeMsg":false,"legIndex":1}],"inboundLegsInfo":[{"taxes":43.22,"basePrice":808.01,"taxesAndFees":50.22,"ceilTotalPrice":859.0,"localeFormattedTaxAndFees":"$50","localeFormatBasePrice":"$808","bookingFee":7.0,"t":858.23,"ft":"$858","fab":"$808","fat":"$858","faf":"$50","tripId":39,"s":2,"c":"DL"},{"taxes":43.22,"basePrice":808.01,"taxesAndFees":50.22,"ceilTotalPrice":859.0,"localeFormattedTaxAndFees":"$50","localeFormatBasePrice":"$808","bookingFee":7.0,"t":858.23,"ft":"$858","fab":"$808","fat":"$858","faf":"$50","tripId":39,"s":2,"c":"AA"}],"airFareBasisCode":"QA00A0NJ","flightFareType":"PUBLISHED","seatCount":1,"totalPrice":858.23,"fare":{"maxLccPrice":0.0,"minLccPrice":0.0,"totalFlightFare":858.23,"currencyCode":"USD","fees":{"amount":7.00,"currency":"USD","double":7.0,"currencyCode":"USD"},"taxes":{"amount":43.22,"currency":"USD","double":43.22,"currencyCode":"USD"},"baseFare":{"amount":808.01,"currency":"USD","double":808.01,"currencyCode":"USD"},"totalFare":{"amount":858.23,"currency":"USD","double":858.23,"currencyCode":"USD"},"totalBaseFare":808.01,"localeTotalBaseFare":"$808","localeTotalFlightTaxes":"$43","totalFlightTaxes":43.22,"localeTotalFlightFare":"$858","localeTotalFees":"$7","localeTotalFlightTaxesAndFees":"$50","totalFees":7.0,"totalTaxesAndFees":{"amount":50.22,"currency":"USD","double":50.22,"currencyCode":"USD"},"flightFare":{"currencyCode":"USD","basePrice":808.01,"taxes":43.22}},"flightId":"39","showFareDisplayName":false,"fareDisplayName":"Published","firstBagFreeAirline":false,"operatedByFeeDisplayFeatureEnabled":false,"hasFare":true,"localeFormatAvgBasePrice":"$808","localeFormatAvgTotalPrice":"$858","stops":2,"bargainFare":false,"fareName":"published","selectedFlightPara":"39:39:FLTIStr","fareTypeCode":"P","airProviderId":2,"ccfeeAirline":"","displayLCCFareName":false,"noOfTicketsLeft":1,"seatPreview":true,"localeFormatAvgTotalTaxesAndFees":"$50","priceIllegal":false,"fareTypeValue":"1","showOBFeeMessageForModule":false,"completeTrip":false,"hasAirlineWithBagFee":false,"hasAirlineWithNoBagFee":false,"isABestTrip":true,"carrierList":["DL","AA"],"isFirstBagFreePos":true},
...so on
json_decode takes a second parameter to return data as an associative array instead of objects. This will solve your problem.
$json_array = json_decode($string,true); // returns 'normal' php array.
$json = json_decode($string); // returns php object hierarchy
http://json.parser.online.fr/ reports that you data ends unexpectedly. Do you actually have the real data? Or if is it wrong, how did you generate it? There might be errors in the generation script cause it is not complete...
There seems to be a misplaced character at the end. Instead of a comma ",", it should be a closing array "]"
You did not say WHY json_decode isn't working. If the JSON is too big to post here just use pastebin or something alike.
I assume the reason it's not working is because you are exceeding the memory allowance for PHP, you can work around this by increasing the max memory PHP can use, eg:
ini_set('memory_limit','16M');
However I wouldn't recommend doing this if this code is going to be ran by a large amount of users. Maybe if you give us some context we can give you an alternative solution which bypasses the need of json_decode.
Related
Extract value from soap response
I need help to extract value from a soap response. https://i.stack.imgur.com/djv5y.jpg What i exactly need is: $username=user $message=success
Maybe include the SOAP response here, that would be helpful for those who come in the future. As for your question are you using a particular language? That would make it easier to answer. If you are looking for a way to view use can use this URL: https://codebeautify.org/xmlviewer
Ok now that I see it, it's fairly easy Assuming you have loaded the SOAP XML into a variable, lets call it $xml_string $xml = simplexml_load_string($xml_string); // Load it as an object $xmlarray = json_decode(json_encode($xml),TRUE); // Change it into an array Then the variables you are looking for is in $username = $xmlarray['UserName']; $message = $xmlarray['response']['MESSAGE']; BTW This solution is found here PHP convert XML to JSON I did it as an array as sometimes objects are a little hard to process. You can easily just do the first line and address it as an object. (If those are the only variables you need then an array works fine. Ex: the 'Plan' data will be messed up in an array as it appears twice) There can be some issues such as the MESSAGE not appearing or the XML returning a failure but I think you should know how to code around missing datum.
Parse this data with PHP
Going to this webstie: http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=AK-47%20%7C%20Redline%20%28Field-Tested%29 Yields this result: {"success":true,"lowest_price":"5,59€ ","volume":"5,688","median_price":"5,92€ "} The result is updated every time the page is refreshed. Using PHP, how would I be able to save the result line and split it up in my code I can use it for other things? Would it be viable/possible to do this about 3000-5000 times from a loop in my code, or would it be too much and crash it? I won't be using all the data from it in my code, just saving it into a database and moving to the next result.
That code is JSON and can be parsed with json_decode $data = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=AK-47%20%7C%20Redline%20%28Field-Tested%29'); $json = json_decode($data); echo $json->volume; As far as looping... why? You would simply load the page 3000+ times in a row. What would be the benefit of that? Perhaps you should consider a cron job instead, which could fetch the data at regular intervals (and not spam the Steam servers)
Your code is in JSON format. So: $content = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=AK-47%20%7C%20Redline%20%28Field-Tested%29'); json_decode($content); Should work correctly
is it possible to receive only partial results in JSON from API calls using PHP?
I have been using PHP to call an API and retrieve a set of data in JSON.I need to make 20 batch calls at once. Therefore, speed is very important. However, I only need the first element, in fact, I only need one data which is right at the beginning of the JSON file, index 0. However, the JSON file returns about 300 sets of data. I have to wait until all the data are ready before I can proceed. I want to speed up my API calls by eliminating redundant datasets. Is it possible for me to receive the first set of the data only without having to wait until everything is ready and then go indexing the first element? excuse my english...thank you in advance.
you could use fopen to get the bytes that are guaranteed to have what you need in it and then use regex to parse it. something like: $max_bytes = 512; $fp = fopen($url, "r") ; $data = "" ; if($fp) { while(!preg_match('/"totalAmount"\:"(.*)"/U', $data, $match)) $data .= stream_get_contents($fp, $max_bytes) ; fclose($fp); if(count($match)){ $totalAmount = $match[1]; } } keep in mind that you cant use the string stored in $data as valid json. It will only be partial
no. json is not a "streamable" format. until you receive the whole string, it cannot be decoded into a native structure. if you KNOW what you need, then you could use string operations to retrieve the portion you need, but that's not reliable nor advisable. Similarly, php will not stream out the text as it's encoded. e.g. consider a case where your data structure is a LOOOONG shallow array $x = array( 0 => blah 1 => blah ... 999,999,998 => blah 999,999,999 => array( .... even more nested data here ...) ); a streaming format would dribble this out as ['blah', 'blah' ............ you could assume that there's nothing but those 'blah' at the top level and output a ], to produce a complete json string: ['blah'.... , 'blah'] and send that, but then you continue encoding and reach that sub array... now you've suddenly got ['blah' ....., 'blah'][ ...sub array here ....] and now it's no longer valid JSON. So basically, json encoding is done in one (long) shot, and not in dibs and drabs, just because you simply cannot know what's coming "later" without parseing the whole structure first.
No. You need to fetch the whole set before parsing and sending the data you need back to the client machine.
json_decode returns null with PHP, NFL API
For some reason json_decode is returning NULL with valid JSON. The json is located here: http://www.nfl.com/liveupdate/scorestrip/scorestrip.json I am getting the file contents of the page (file_get_contents) and then running json_decode on it. Also tried stripslashes b/c of magic quotes. Thanks in advance, Phil $json = file_get_contents('http://www.nfl.com/liveupdate/scorestrip/scorestrip.json'); var_dump(json_decode($json));
The empty array elements are invalid JSON, ie the multiple commas with no values between ["Thu","7:30","Final",,"BAL","6","PHI","13",,,"55424",,"PRE1","2011"] ^ Here ^ Here ^Here You could try inserting empty strings between multiple commas (thanks hakre), ie $json = preg_replace('/,(?=,)/m', ',""', $json); // I'm dubious about this working, don't have PHP to test but it works in JS I don't know if this messes up your ability to continue using the data though
I came the same conclusion as Phil, but it took me about 5 minutes longer. : ) However, I noticed a problem in his output... it is dropping a field. I don't like doing this kind of pattern detection in RegEx, only because I think it is harder to maintain. In this case, I just run it through a while loop. while (strpos($json,',,')) { $json = str_replace(',,',',"",', $json); } I'm sure Phil can come up with a proper RegEx, but this solution will maintain the right # of entries & their ordinal positions.
For anyone looking in 2014. The new feed data link should be: http://www.nfl.com/liveupdate/scores/scores.json
Not able to parse this json
I am trying to parse the json output from http://www.nyc.gov/portal/apps/311_contentapi/services/all.json And my php json_decode returns a NULL I am not sure where the issue is, I tried running a small subset of the data through JSONLint and it validated the json. Any Ideas?
The error is in this section: { "id":"2002-12-05-22-24-56_000010083df0188b4001eb56", "service_name":"Outdoor Electric System Complaint", "expiration":"2099-12-31T00:00:00Z", "brief_description":"Report faulty Con Edison equipment, including dangling or corroded power lines or "hot spots."" } See where it says "hot spots." in an already quoted string. Those "'s should've been escaped. Since you don't have access to edit the JSON perhaps you could do a search for "hot spots."" and replace it with \"hot spots.\"" like str_replace('"hot spots.""', '\\"hot spots.\\""\, $str); for as long as that's in there. Of course that only helps if this is a one time thing. If the site continues to make errors in their JSON output you'll have to come up with something more complex.
What I did to identify the errors in the JSON ... Since faulty quoting is the first thing to look for, I downloaded the JSON to a text file, opened in a text editor (I used vim but any full featured editor would do), ran a search and replace that removed all characters except double-quote and looked at the result. It was clear that correct lines should have 4 double-quotes so I simply searched for 5 double-quotes together and found the first bad line. I noted the line number and then undid the search and replace to get the original file back and looked at that line. This gives you what you need to get the developers of the API to fix the JSON. Writing code to automatically fix the bad JSON before giving it to json_decode() would be quite a bit harder but doable using techniques like those in another answer.
According to the PHP manual: In the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error. Try calling it to see where the error is.