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;
Related
This is the JSON
{
"table-name": "Kiwi",
"created-on": "November 20, 2021",
"columns": {
"Info": {
"type": "longtext",
"extra": ""
},
"Status": {
"type": "droplist",
"extra": ""
},
"Task": {
"type": "text",
"extra": ""
}
},
"data": [
{
"Name": "Team Reports",
"Info": "Submitting marketing materials reports",
"Status": "Completed"
},
{
"Name": "Fabia HR",
"Info": "Brian asking for a report",
"Status": "Pending"
},
{
"Name": "Fabia",
"Info": "Meeting with CEO #cafe 9:00",
"Status": "Cancelled"
}
]
}
And I was trying to achieve to rename the "Info" into "Description" without losing its value and array position. I'm not very familiar with array_replace , that seems my code is not working. Please share some codes, it will be much appreciated.
Here my PHP code I tried
<?PHP
$jsn = file_get_contents('./test.json');
$arr = json_decode($jsn, true);
$newArray= [];
foreach($arr['data'] as $row){
$row['Description'] = $row['Info'];
array_push($newArray, $row);
}
//print_r($newArray);
array_replace($arr['data'],$newArray);
echo json_encode($arr, JSON_PRETTY_PRINT);
Thank you so much for your attention and advance help. It will really gonna save me some time. Noob here and I'm still learning things about PHP and JSON
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'];
$url="https://alcurex.com/api/market.php?pair=mrc_ltc&price=buy";
$json = file_get_contents($url);
$data = json_decode($json);
echo $json;
var_dump($data);
Results in a null response. However when you visit the page you get a html read out of
{
"pair": MRC_LTC,
"time": "2014-07-23 07:34:54,
"price": 0.00000017,
"volume": 558.99741176,
"type": Buy
},
Response is not valid JSON format.
{ "pair": MRC_LTC, "time": "2014-07-23 07:34:54, "price": 0.00000017, "volume": 558.99741176, "type": Buy },
You could check it from Firefox console, correct JSON should be this:
var s = {
"pair": "MRC_LTC",
"time": "2014-07-23 07:34:54",
"price": 0.00000017,
"volume": 558.99741176,
"type": "Buy"
};
Errors:
Missing " for pair, time and type
Unnecessary comma at the end of string
You have three problems with this JSON:
pair value is not a valid type, if it should be string, it's missing quotes
time has string start quote but does not end it
type value is not a valid type, if it should be string, it's missing quotes
This is a valid JSON:
{
"pair": "MRC_LTC",
"time": "2014-07-23 07:34:54",
"price": 0.00000017,
"volume": 558.99741176,
"type": "Buy"
}
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.
I have the following url:
https://graph.facebook.com/123456/likes?access_token=__ACCESS_TOKEN__&format=json
which I then do:
$likesList = json_decode(file_get_contents("https://graph.facebook.com/123456/likes?access_token=$access_token&format=json"),true);
which produces e.g.
{
"data": [
{
"name": "yo yo yo",
"category": "Entertainer",
"id": "45640987076",
"created_time": "2012-04-18T16:14:09+0000"
},
{
"name": "Tony Smith",
"category": "Musician/band",
"id": "456456456456",
"created_time": "2012-02-22T06:56:18+0000"
},
{
"name": "Stations",
"category": "Company",
"id": "567657567",
"created_time": "2012-01-30T23:08:39+0000"
}
]
}
and I then want to list e.g. all the names returned so:
foreach ($likesList->data as $element2){
$name = $element2[name];
echo $name;
}
But it's empty?
See this visualization of your data structure.
As you are receiving an array, you need $list["data"] and not $list->data. Also don't forget to quote the array key "name".
foreach ($likesList['data'] as $element2){
$name = $element2['name'];
echo $name;
}
After json_decode with parameter true you will have associative array. You can access to value by string key. Like in example above.