I have a string like the below in a variable called $CPU. The text is below, my target is to pull specific element of the below text.
[{
"#attributes": {
"name": "processors"
},
"entry": ["2", "1", "successful"],
"entries": {
"#attributes": {
"name": "processor"
},
"entry": ["Intel(R) Core(TM) i5-7200U CPU # 2.50GHz", "GenuineIntel", "2500000000", "3100000000", "2", "4", "true", "9", "100 MHz", "7"],
"entries": {
"#attributes": {
"name": "caches"
},
"entries": [{
"#attributes": {
"name": "cache"
},
"entry": ["Level 1 Cache", "131072"]
}, {
"#attributes": {
"name": "cache"
},
"entry": ["Level 2 Cache", "524288"]
}, {
"#attributes": {
"name": "cache"
},
"entry": ["Level 3 Cache", "3145728"]
}]
}
}
}]
The thing I want to extract specifically is this bit is the "Intel(R) Core(TM) i5-7200U CPU # 2.50GHz" however I'm really unsure how to achieve this easily.
It's a JSON! So if it works for JS it will work for PHP:
$arr = json_decode($CPU, true);
echo $arr[0]["entries"]["entry"][0];
var x = [{
"#attributes": {
"name": "processors"
},
"entry": ["2", "1", "successful"],
"entries": {
"#attributes": {
"name": "processor"
},
"entry": ["Intel(R) Core(TM) i5-7200U CPU # 2.50GHz", "GenuineIntel", "2500000000", "3100000000", "2", "4", "true", "9", "100 MHz", "7"],
"entries": {
"#attributes": {
"name": "caches"
},
"entries": [{
"#attributes": {
"name": "cache"
},
"entry": ["Level 1 Cache", "131072"]
}, {
"#attributes": {
"name": "cache"
},
"entry": ["Level 2 Cache", "524288"]
}, {
"#attributes": {
"name": "cache"
},
"entry": ["Level 3 Cache", "3145728"]
}]
}
}
}]
console.log(x[0]["entries"]["entry"][0])
I have a JSON response that I'm trying to gather data from. Most of the data is not needed but the way it is structured I'm having a tough time figuring out how to just get the data I need. The strings and values are paired kinda weird.
I need to be able to pull the data using PHP, and I need the info from the characteristic fields. Specifically, I need to pull the value for "qualificationTier".
What would I use to pull that string/value from this JSON response?
{
"externalId": "testinc-334",
"site": [
{
"id": "site-281",
"href": "qualification/site-281",
"description": "Service Qualification Successful",
"interactionDate": "2022-01-22T21:09:53.843Z",
"interactionDateComplete": "2022-01-22T21:09:59.873Z",
"interactionStatus": "Success",
"place": {
"id": "00000TBKM3",
"href": "site/00000TBKM3",
"characteristic": [
{
"name": "latitude",
"value": "32.98972"
},
{
"name": "longitude",
"value": "-117.271838"
},
{
"name": "region",
"value": "INREGION"
},
{
"name": "npa",
"value": "858"
},
{
"name": "nxx",
"value": "720"
},
{
"name": "lata",
"value": "732"
},
{
"name": "swCLLI",
"value": "DLMRCA12"
},
{
"name": "buildingCLLI",
"value": "SOBHCAWE"
},
{
"name": "ILECVendorName",
"value": "PACIFIC BELL"
},
{
"name": "affiliateName",
"value": "AT&T California"
},
{
"name": "unitType",
"value": "STE"
},
{
"name": "unitValue",
"value": "200"
},
{
"name": "standardAddress",
"value": "215 S HIGHWAY 101 STE 200,SOLANA BEACH,CA,92075-1847"
},
{
"name": "qualificationTier",
"value": "Tier 2"
},
{
"name": "fiberFastQualIndicator",
"value": "Y"
},
{
"name": "fiberStatus",
"value": "Fiber Lit"
}
]
},
"serviceAvailability": {
"service": [
{
"id": "adi0001",
"href": "/service/adi0001",
"name": "Customer Managed ADI",
"availability": "available",
"serviceabilityDate": "2022-01-22T21:09:59.880Z",
"serviceSpecification": [
{
"id": "miseth0000002uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000002uo",
"name": "Customer Managed ADI 2 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "2"
},
{
"name": "uploadSpeed",
"value": "2"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000002ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000002ue",
"name": "Customer Managed ADI 2 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "2"
},
{
"name": "uploadSpeed",
"value": "2"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000005uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000005uo",
"name": "Customer Managed ADI 5 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "5"
},
{
"name": "uploadSpeed",
"value": "5"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000005ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000005ue",
"name": "Customer Managed ADI 5 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "5"
},
{
"name": "uploadSpeed",
"value": "5"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000010uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000010uo",
"name": "Customer Managed ADI 10 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "10"
},
{
"name": "uploadSpeed",
"value": "10"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000010ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000010ue",
"name": "Customer Managed ADI 10 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "10"
},
{
"name": "uploadSpeed",
"value": "10"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000020uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000020uo",
"name": "Customer Managed ADI 20 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "20"
},
{
"name": "uploadSpeed",
"value": "20"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000020ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000020ue",
"name": "Customer Managed ADI 20 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "20"
},
{
"name": "uploadSpeed",
"value": "20"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000050uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000050uo",
"name": "Customer Managed ADI 50 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "50"
},
{
"name": "uploadSpeed",
"value": "50"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000050ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000050ue",
"name": "Customer Managed ADI 50 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "50"
},
{
"name": "uploadSpeed",
"value": "50"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000100uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000100uo",
"name": "Customer Managed ADI 100 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "100"
},
{
"name": "uploadSpeed",
"value": "100"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
```
Check if this works.
<?php
$data = json_decode($your_json);
$qualificationTier = "qualificationTier";
// qualificationTier name is at position 13 in characteristic array
$QualificationTierName = $data->site->place->characteristic[13]->name;
$QualificationTierValue = "";
// Confirm indeed if the string name at position 13 in characteristics is indeed qualificationTier before retrieving the value at the same position
if(strcmp($qualificationTier, $QualificationTierName) == 0 ){
// qualificationTier value is at position 13 in characteristic array
$QualificationTierValue = $data->site->place->characteristic[13]->value;
echo "QualificationTier Name : " + $QualificationTierName;
echo "QualificationTier Value : " + $QualificationTierValue;
}
?>
A simple way would be to loop the array and get value when the name matches your field. You should first decode the json response so you will have an object to work with. Then just loop and set the variable when you have a match.
<?php
$response = json_decode($_POST['json-name']);
$qualificationTier = null;
foreach ($response->site->place->characteristic as $c) {
if ($c->name === 'qualificationTier') {
$qualificationTier = $c->value;
}
}
i need to combine two jsons in a job task, according to the person's location. I have a Json with people from location type 1, and another from location type 2.
I need to combine in a new array for each time they have the same location, and they are repeated and separate according to type. I tried to do it with a foreach inside the other, but with no success. Here is an example of JSON.
Json 1:
[
{
"id": 1,
"name": "Jacob",
"place": "Brazil",
"type": 1
},
{
"id": 2,
"name": "Izac",
"place": "Brazil",
"type": 1
},
{
"id": 3,
"name": "Anran",
"place": "Brazil",
"type": 1
},
{
"id": 4,
"name": "Irbur",
"place": "Brazil",
"type": 1
},
{
"id": 5,
"name": "Lusos",
"place": "Brazil",
"type": 1
},
{
"id": 6,
"name": "Gamio",
"place": "Brazil",
"type": 1
},
{
"id": 7,
"name": "Nubeil",
"place": "Brazil",
"type": 1
},
{
"id": 8,
"name": "Usgon",
"place": "Brazil",
"type": 1
},
{
"id": 15,
"name": "Fikis",
"place": "England",
"type": 1
}
]
Json 2:
[
{
"id": 9,
"name": "Gipin",
"place": "Brazil",
"type": 0
},
{
"id": 10,
"name": "Paoir",
"place": "Brazil",
"type": 0
},
{
"id": 11,
"cia": "Mutue",
"place": "Brazil",
"type": 0
},
{
"id": 12,
"name": "Ziefel",
"place": "England",
"type": 0
},
{
"id": 13,
"name": "Liedo",
"place": "England",
"type": 0
},
{
"id": 14,
"name": "Vicis",
"place": "England",
"type": 0
}
]
the result might look something like this:
{
"groups": [ // groups (same place)
{
"tipe1": [ // groups type 1: same place
{
"id": 1,
"name": "Jacob",
"place": "Brazil"
},
{
"id": 1,
"name": "Jacob",
"place": "Brazil"
}
]
"tipe2": [ // groups type 2: same place
{
"id": 11,
"name": "Mutue",
"place": "Brazil"
},
]
},
{
"tipe1": [
{
"id": 15,
"name": "Fikis",
"place": "England"
}
]
"tipe2": [
{
"id": 14,
"name": "Vicis",
"place": "England"
},
]
}
],
}
The rule for grouping is, two or more items of type 1 can be grouped with 1 of type 2, in reverse as well. It can be thought of as outward flights, and back flights.
Can someone teach me how can i access each of the category element in the data array?? PS: I post this JSON using Postman and try to manipulate it in a laravel project controller method.
But i keep on getting this error:
ErrorException: Trying to get property 'data' of non-object
There is no problem with my routes as i have successful using get and post request for other simple process.
I also use Content-Type with application/json header to send this JSON using the Postman software.
My controller method:
public function requestStore(Request $request) {
//
}
JSON:
{
"id": "126612213872902",
"name": "Jack Daniel",
"likes": {
"data": [
{
"category": "Steakhouse",
"id": "330668300343348"
},
{
"category": "Steakhouse",
"id": "793734850733472"
},
{
"category": "Steakhouse",
"id": "650634241797601"
},
{
"category": "Steakhouse",
"id": "146621545355340"
},
{
"category": "Italian Restaurant",
"id": "146805911480"
},
{
"category": "Ice Cream Shop",
"id": "545851225458015"
},
{
"category": "Fast Food Restaurant",
"id": "24540959832"
},
{
"category": "Fast Food Restaurant",
"id": "478890372318431"
},
{
"category": "Fast Food Restaurant",
"id": "78934590040"
},
{
"category": "Korean Restaurant",
"id": "553600018012159"
},
{
"category": "Korean Restaurant",
"id": "388180681558608"
},
{
"category": "Chinese Restaurant",
"id": "288066224573571"
},
{
"category": "Chinese Restaurant",
"id": "77730698813"
},
{
"category": "Steakhouse",
"id": "80555139083"
},
{
"category": "Steakhouse",
"id": "276973702580"
},
{
"category": "Halal Restaurant",
"id": "407606596237025"
},
{
"category": "Malaysian Restaurant",
"id": "337074526415500"
},
{
"category": "Malaysian Restaurant",
"id": "607366992933940"
},
{
"category": "Malaysian Restaurant",
"id": "523902944321612"
},
{
"category": "Malaysian Restaurant",
"id": "194113415406"
},
{
"category": "Malaysian Restaurant",
"id": "177159752316413"
},
{
"category": "Malaysian Restaurant",
"id": "705777492887089"
},
{
"category": "Malaysian Restaurant",
"id": "1545544055681553"
},
{
"category": "Steakhouse",
"id": "558487800860920"
},
{
"category": "Steakhouse",
"id": "129509727619"
}
],
"paging": {
"cursors": {
"before": "MzMwNjY4MzAwMzQzMzQ4",
"after": "MTI5NTA5NzI3NjE5"
}
}
}
}
Assuming you're posting raw json through Postman like this don't forget to set type to JSON
Access the data like you would an array instead
Route::post('/test', function () {
$data = request()->all();
foreach ($data['likes']['data'] as $key => $value) {
dd($value['category']);
}
});
Result:
I hope this helps
I am looking for a way to retrieve the number of likes of a Facebook post, based on its post_id. I got hold of a php code from this forum itself..and it's something like
<?php
function fetchUrl($url){
return file_get_contents($url);
}
$json_object = fetchUrl("https://graph.facebook.com/{post_id}/likes?access_token={token}&limit=5000000"); //
$feedarray = json_decode($json_object, true);
$likesNum = count($feedarray['data']); // return the number of items in `data` array
print $likesNum;
?>
But the problem is this method does not retrieve the total likes of a post, since likes are displayed in blocks of 1000, after which theres another link to a different page containing the next set of 1000 likes and so on.
Is there a way to get the total number of likes of a facebook post by a single query?
Add the summary flag ** summary=true**
"https://graph.facebook.com/{post_id}/likes?access_token={token}&summary=true"
{
"data": [
{
"id": "663342380429664",
"name": "Luis Mendoza"
},
{
"id": "1532100840406448",
"name": "Sakazuki Akainu"
},
{
"id": "780666205412913",
"name": "Joaito KoRn"
},
{
"id": "1060933433919925",
"name": "Adrian Sosa"
},
{
"id": "860704407276452",
"name": "Sarah Rosenstrauch"
},
{
"id": "1947974762009431",
"name": "David Prieto"
},
{
"id": "804864302928112",
"name": "Ronal Ortega"
},
{
"id": "1505075359814934",
"name": "Gonzalo Larzen"
},
{
"id": "1431207613804483",
"name": "Victor Clc"
},
{
"id": "508785009283633",
"name": "Rxdry EzDe Cerrx Mcmxii"
},
{
"id": "435355413265946",
"name": "Ángel Fernando Huillca Alonso"
},
{
"id": "163773913961445",
"name": "Pelado Miguel Pin Macias"
},
{
"id": "1479227465674392",
"name": "Releck Solitario"
},
{
"id": "161610054193539",
"name": "MD Sahin MD Sahin"
},
{
"id": "798431050242097",
"name": "Brian Nahuel"
},
{
"id": "624869574305480",
"name": "Saul Alfredo"
},
{
"id": "1642733362665392",
"name": "Junior Zurita"
},
{
"id": "134907406871404",
"name": "Wil Peña"
},
{
"id": "10153052770952668",
"name": "Miguel Peña Cáceres"
},
{
"id": "1461494580846182",
"name": "Darian Suarez"
},
{
"id": "365762500250317",
"name": "Igarashi Ganta"
},
{
"id": "750032685093387",
"name": "Camila Barbé"
},
{
"id": "781013541941152",
"name": "Gonzalo Nievas"
},
{
"id": "756520927743339",
"name": "Jonathan C. Duran Cuellar"
},
{
"id": "1504488093199860",
"name": "Maxi Russo"
}
],
"paging": {
"cursors": {
"before": "NjYzMzQyMzgwNDI5NjY0",
"after": "MTUwNDQ4ODA5MzE5OTg2MAZDZD"
},
"next": "https://graph.facebook.com/v2.3/1009501939072385/likes?access_token=TOKEN..."
},
"summary": {
"total_count": 4303
}
}