How to modify large json structure for Google Map using PHP - php

I have a Google Map using the Google Map API drawing polylines from a .json file. A portion of the file structure can be seen below, it just repeats for each polyine feature. It was created in GIS and converted to a .geojson and then json.
I am looking to allow a user to modify specific properties of a feature through a mapping interface. I have the iterface and user interactions all built out but am having trouble modifying the .json file.
I have tried the following:
PHP (Note: I am hard-coding some values to test/get it to work)
$jsonString = file_get_contents('../json/hartford.json');
$data = json_decode($jsonString, true);
foreach ($data as $key => $entry) {
if ($entry['UID'] == 25301) {
$data[$key]['RENDER_CL'] = "99";
}
}
$newJsonString = json_encode($data);
file_put_contents('../json/hartford.json', $newJsonString);
Errors:
Warning: Illegal string offset 'UID' on line...
Notice: Undefined index: UID on line...
Thoughts:
I feel I am not going deep enough in to the json "tree" to access the properties. I have not modified json data before so I am a bit lost. Should I somehow be going deeper into the structure? UID is unique for each item. Thanks.
JSON file
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [{
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25301
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.70621818192563523552962578833103179931640625, 41.7494854544135023388662375509738922119140625], [-72.7059645455619971698979497887194156646728515625, 41.74953636350443275659927166998386383056640625], [-72.7057163637438179648597724735736846923828125, 41.749591818049879066165885888040065765380859375], [-72.705553636471080380943021737039089202880859375, 41.749633636231692435103468596935272216796875], [-72.7053663637438063460649573244154453277587890625, 41.749688181686252619329025037586688995361328125], [-72.705190909198364579424378462135791778564453125, 41.749742727140784381845151074230670928955078125], [-72.70496727283472182534751482307910919189453125, 41.749821818049866806177305988967418670654296875], [-72.704716363743813190012588165700435638427734375, 41.749924545322613766984432004392147064208984375], [-72.70461181828926555681391619145870208740234375, 41.74996818168625623002299107611179351806640625]]]
}
}, {
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25302
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
}
},

You are accessing your object's property incorrectly. the correct way would be $entry['properties']['UID'].
Example
<?php
$json =
'{
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25302
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
}
}';
$obj = json_decode($json, true);
print $obj['properties']['UID'];

Related

Pick off individual values in Nested json

Give the supplied JSON I want to extract the latitude and longitude coordinates only.
"vectorQuery": {
"layers": {
"50772": {
"crs": {
"type": "name",
"properties": {
"name": "EPSG:4326"
}
},
"field_names": ["id", "appellation", "affected_surveys", "parcel_intent", "topology_type", "statutory_actions", "land_district", "titles", "survey_area", "calc_area"],
"type": "FeatureCollection",
"features": [{
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[174.8592712667, -36.9675741167],
[174.8593271167, -36.9673525333],
[174.8593513167, -36.9672686333],
[174.8592025167, -36.9671776333],
[174.8591615833, -36.9672144833],
[174.8588608833, -36.9674851833],
[174.8592451667, -36.9678935833],
[174.8592699833, -36.9679199],
[174.8594188, -36.9680780333],
[174.8597915667, -36.9678483167],
[174.85991985, -36.9677692333],
[174.8599389, -36.96770445],
[174.8596869333, -36.9676552667],
[174.8596362667, -36.9676453833],
[174.8592712667, -36.9675741167]
]
]
]
},
"distance": 0,
"type": "Feature",
"properties": {
"id": 5073537,
"appellation": "Lot 2 DP 64503",
"affected_surveys": "DP 64503",
"parcel_intent": "DCDB",
"topology_type": "Primary",
"statutory_actions": null,
"land_district": "North Auckland",
"titles": "NA20A/1419",
"survey_area": 3688.0,
"calc_area": 3686.0
},
"id": 995125
}]
}
}
}
}
I actually just want to get the coordinates. I can pick these of these by ...
print_r($arr['vectorQuery']['layers']['50772']['features'][0]['geometry']['coordinates'][0][0]);
The issue being that the key 50772 can change each request. Is there a method of traversing directly to the coordinates?

JSON Post PHP (TypeForm)

I have never used JSON before so apologies if this is a simple request.
I have a webhook setup that sends me a JSON Post (Example Below) - I want to extract the two answers from this "text":"250252" & {"label":"CE"}
{
"event_id": "1",
"event_type": "form_response",
"form_response": {
"form_id": "VpWTMQ",
"token": "1",
"submitted_at": "2018-05-22T14:11:56Z",
"definition": {
"id": "VpWTMQ",
"title": "SS - Skill Change",
"fields": [
{
"id": "kUbaN0JdLDz8",
"title": "Please enter your ID",
"type": "short_text",
"ref": "9ac66945-899b-448d-859f-70562310ee5d",
"allow_multiple_selections": false,
"allow_other_choice": false
},
{
"id": "JQD4ksDpjlln",
"title": "Please select the skill required",
"type": "multiple_choice",
"ref": "a24e6b58-f388-4ea9-9853-75f69e5ca337",
"allow_multiple_selections": false,
"allow_other_choice": false
}
]
},
"answers": [
{
"type": "text",
"text": "250252",
"field": {
"id": "kUbaN0JdLDz8",
"type": "short_text"
}
},
{
"type": "choice",
"choice": {
"label": "CE"
},
"field": {
"id": "JQD4ksDpjlln",
"type": "multiple_choice"
}
}
]
}
}
I have this currently in my PHP file:
$data = json_decode(file_get_contents('php://input'));
$ID = $data->{"text"};
$Skill = $data->{"label"};
This does not work and all I get is null - Any help would really be appreciated, Thank You.
You need to look at the JSON object you're receiving to know the structure of the object you're receiving after using json_decode, what you're trying to get is in $data->form_response->answers, So you can have a variable for easier access:
$answers = $data->form_response->answers;
remember $answers is an array
So to achieve what you're trying to get, you can do:
$data = json_decode(file_get_contents('php://input'));
$answers = $data->form_response->answers;
$ID = $answers[0]->text;
$Skill = $answers[1]->choice->label;

How to read JSON results from PHP

How to access element of below json output using php ..
{
"ISBN:9781430215752": {
"bib_key": "ISBN:9781430215752",
"preview":
"restricted",
"preview_url":
"https://archive.org/details/linuxrecipesforo00kuhn",
"info_url": "https://openlibrary.org/books/OL23936576M/Linux_recipes_for_Oracle_DBAs",
"details": {
"lc_classifications": ["QA76.9.D3 K84 2009"],
"latest_revision": 2,
"ocaid": "linuxrecipesforo00kuhn",
"contributions": ["Kim, Charles.", "Lopuz, Bernard."],
"source_records": ["marc:marc_loc_updates/v37.i44.records.utf8:10470755:1047"],
"title":
"Linux recipes for Oracle DBAs",
"languages": [{
"key": "/languages/eng"
}],
"subjects": ["Linux", "Oracle (Computer file)", "Relational databases", "Database management"],
"publish_country": "cau",
"by_statement": "Darl Kuhn, Charles Kim, Bernard Lopuz.",
"type": {
"key": "/type/edition"
},
"revision": 2,
"other_titles": ["Linux recipes for Oracle DataBase Administrators"],
"publishers": ["Apress", "Distributed to the book trade by Springer-Verlag"],
"last_modified": {
"type": "/type/datetime",
"value": "2014-04-06T06:55:36.956977"
},
"key": "/books/OL23936576M",
"authors": [{
"name": "Darl Kuhn",
"key": "/authors/OL1484587A"
}],
"publish_places": ["Berkeley, CA", "New York"],
"oclc_number": ["243543902"],
"pagination": "xxv, 501 p. :",
"created": {
"type": "/type/datetime",
"value": "2009-11-24T23:42:39.524606"
},
"dewey_decimal_class": ["005.75/65 22", "005.26/8"],
"notes": {
"type": "/type/text",
"value": "Includes index."
},
"number_of_pages": 501,
"isbn_13": ["9781430215752"],
"lccn": ["2009277832"],
"isbn_10": ["1430215755"],
"publish_date": "2008"
}
}
}
I tried using below code,
it doesn't work.
$json = json_decode($body);
echo $json->ISBN:9780980200447->info_url;
It's giving an error..
Is there any other easy way to read all elements?
You are on the right track. : is just an invalid character for a property and must be handled special.
echo $json->{'ISBN:9781430215752'}->info_url;
will get you the expected result.
You may also decode as an associative array
$json = json_decode($body, true);
echo $json['ISBN:9781430215752']['info_url'];
I would probably stick to this approach rather than the object oriented, since encapsulating keys makes code less readable
You must use
echo $json->{'ISBN:9780980200447'}->info_url;
since 'ISBN:9780980200447' is your class name.
Reference: php.net
<?php
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
?>
Use braces like this:
echo $json->{'ISBN:9781430215752'}->info_url;

Decode json in php and create variables

I a trying to decode a json callback.
The json code is posted to callback.php - Here is an example of the json:
{
"order": {
"id": "5RTQNACF",
"created_at": "2012-12-09T21:23:41-08:00",
"status": "completed",
"total_btc": {
"cents": 100000000,
"currency_iso": "BTC"
},
"total_native": {
"cents": 1253,
"currency_iso": "USD"
},
"custom": "order1234",
"receive_address": "1NhwPYPgoPwr5hynRAsto5ZgEcw1LzM3My",
"button": {
"type": "buy_now",
"name": "Alpaca Socks",
"description": "The ultimate in lightweight footwear",
"id": "5d37a3b61914d6d0ad15b5135d80c19f"
},
"transaction": {
"id": "514f18b7a5ea3d630a00000f",
"hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"confirmations": 0
},
"customer": {
"email": "coinbase#example.com",
"shipping_address": [
"John Smith",
"123 Main St.",
"Springfield, OR 97477",
"United States"
]
}
}
}
I can echo the json and get the following response:
{"order""id":null,"created_at":null,"status":"completed","total_btc":{"cents":100000000,"currency_iso":"BTC"},"total_native":{"cents":83433,"currency_iso":"USD"},"custom":"123456789","receive_address":"1A2qsxGHo9KjtWBTnAopTwUiBQf2w6yRNr","button":{"type":"buy_now","name":"Test Item","description":null,"id":null},"transaction":{"id":"52d064b59eeb59985e00002c","hash":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b","confirmations":0}}}
However if I try to decode the json using the following:
$array = json_decode($jsonString, true);
echo $array;
I get the following response: "200 Array"
I want to be able turn each json parameter in to a php variable.
You can access the variables within $array, for example by doing:
echo $array['custom']; // prints out "order1234"
You don't want to extract the variables directly into the local lexical scope of your program as that would create security concerns. Just use the data as indicated in the snippet above.

points stored in geoJSON do not show up on map

I've got a geoJSON file consisting of:
{
"type": "FeautureCollection",
"features": [
{
"type": "Feature",
"id": "1",
"geometry": {
"type": "Point",
"coordinates": [
5.709531,
50.855802
]
}
},
{
"type": "Feature",
"id": "2",
"geometry": {
"type": "Point",
"coordinates": [
5.709426,
50.855798
]
}
}
]
}
This JSON file is constructed in a php script:
$ret = array();
$ret['type'] = "FeautureCollection";
$ret['features'] = array();
$f = array();
$f['type'] = 'Feature';
$f['id'] = $p['id'];
$f['geometry'] = array();
$f['geometry']['type'] = 'Point';
$f['geometry']['coordinates'] = array(floatval($p['lat']), floatval($p['lon']));
$ret['features'][] = $f;
echo json_encode($ret);
When i try to load it through the polymaps.org framework none of the points are displayed.
besides the standard stuff to load the map I use this javascript to parse the geoJSON.
map.add(po.geoJson()
.url('geojson/c.geo.php')
.id('test')
);
The CSS is the following, so that the points should look like a red-circle:
#test {
fill: lightcoral;
fill-opacity: .5;
stroke: brown;
}
The strange thing is, that when I alter the geojson file pointer to one constructed by a GIS application it works. So I bet my geoJSON is corrupted. But when I run it through http://jsonlint.com/ it validates.
Does anybody know how this is possible?
Not sure, but I think I have ever face the same problem with another framework:http://leaflet.cloudmade.com/
In my case, it is a matter of view position (e.g: you feature is on America, but your map show Africa). Also in cloudmade-leaflet, they use latitude-longitude instead of longitude-latitude.
You can check if your map view the correct area.

Categories