I'm developing website using weatherapi. I intend to fetch weather using cityid which is in citylist.json file. Users wil enter city name, I was hoping to extract cityid from this json and use the cityid to fetch weather using api. But there is some error in json.
if (file_exists('citylist.json')) {
$cityArray = file_get_contents("citylist.json");
$cityAA = json_decode($cityArray,true);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
print_r($cityAA);
}
The json is around 12mb. Here are first few lines
{
"_id": 14256,
"coord": {
"lon": 48.570728,
"lat": 34.790878
},
"country": "IR",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 132142
},
"langs": [{
"de": "Azad Shahr"
},
{
"fa": "آزادشهر"
}],
"name": "Azadshahr",
"stat": {
"level": 1.0,
"population": 514102
},
"stations": [{
"id": 7030,
"dist": 9,
"kf": 1
}],
"zoom": 10
}{
"_id": 18918,
"coord": {
"lon": 34.058331,
"lat": 35.012501
},
"country": "CY",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 146615
},
"langs": [{
"en": "Protaras"
},
{
"ru": "Протарас"
}],
"name": "Protaras",
"stat": {
"level": 1.0,
"population": 20230
},
"stations": [{
"id": 5448,
"dist": 42,
"kf": 1
}],
"zoom": 6
}
I've tried jsonlint but the file is too big I guess. The var_dump suggests Syntax error, malformed JSON. I cannot post the image as i'm not allowed.
How can I find out where the json is malformed?
This site can help you: http://jsonlint.com/
In the example provided you can't just pass 2 objects like you did. You need to put it inside an array.
[obj,obj2]
Fixed:
[{
"_id": 14256,
"coord": {
"lon": 48.570728,
"lat": 34.790878
},
"country": "IR",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 132142
},
"langs": [{
"de": "Azad Shahr"
}, {
"fa": "آزادشهر"
}],
"name": "Azadshahr",
"stat": {
"level": 1.0,
"population": 514102
},
"stations": [{
"id": 7030,
"dist": 9,
"kf": 1
}],
"zoom": 10
}, {
"_id": 18918,
"coord": {
"lon": 34.058331,
"lat": 35.012501
},
"country": "CY",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 146615
},
"langs": [{
"en": "Protaras"
}, {
"ru": "Протарас"
}],
"name": "Protaras",
"stat": {
"level": 1.0,
"population": 20230
},
"stations": [{
"id": 5448,
"dist": 42,
"kf": 1
}],
"zoom": 6
}]
It looks like the file contains multiple JSON objects but not well integrated.
This is valid JSON:
{
"_id": 14256,
"coord": {
"lon": 48.570728,
"lat": 34.790878
},
"country": "IR",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 132142
},
"langs": [{
"de": "Azad Shahr"
},
{
"fa": "آزادشهر"
}],
"name": "Azadshahr",
"stat": {
"level": 1.0,
"population": 514102
},
"stations": [{
"id": 7030,
"dist": 9,
"kf": 1
}],
"zoom": 10
}
And this is too:
{
"_id": 18918,
"coord": {
"lon": 34.058331,
"lat": 35.012501
},
"country": "CY",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 146615
},
"langs": [{
"en": "Protaras"
},
{
"ru": "Протарас"
}],
"name": "Protaras",
"stat": {
"level": 1.0,
"population": 20230
},
"stations": [{
"id": 5448,
"dist": 42,
"kf": 1
}],
"zoom": 6
}
But how they are put together, it is not. That might be a problem of the JSON file imported or if you are generating it by yourself from the API.
A valid JSON would be formed like an array of those objects:
[{
"_id": 14256,
"coord": {
"lon": 48.570728,
"lat": 34.790878
},
"country": "IR",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 132142
},
"langs": [{
"de": "Azad Shahr"
}, {
"fa": "آزادشهر"
}],
"name": "Azadshahr",
"stat": {
"level": 1.0,
"population": 514102
},
"stations": [{
"id": 7030,
"dist": 9,
"kf": 1
}],
"zoom": 10
}, {
"_id": 18918,
"coord": {
"lon": 34.058331,
"lat": 35.012501
},
"country": "CY",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 146615
},
"langs": [{
"en": "Protaras"
}, {
"ru": "Протарас"
}],
"name": "Protaras",
"stat": {
"level": 1.0,
"population": 20230
},
"stations": [{
"id": 5448,
"dist": 42,
"kf": 1
}],
"zoom": 6
}]
PS: You can use http://jsonlint.com/ to check valid JSON.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With