Mysql search in json array with Like operator - php

I have a problem with MySQL json type. I want to search in mysql json array and get rows. My json data this
{
"id": 361,
"email": "example#outlook.com",
"code": null,
"username": null,
"permissions": null,
"created_at": "2019-03-01 16:09",
"updated_at": "2019-03-01 16:09",
"user_profile": {
"id": 361,
"name": "Jhon",
"surname": "Doe",
"father_name": "NED",
"birthday": "1994-12-15",
"fin_code": "6A56BS7",
"ssn": "AAA12830157",
"account_number": "account123",
"country": "USA",
"city": "NEW YORK",
"address": "EXample r",
"address_n": "Khani/Bina",
"mobile_phone": "(($717865643",
"phone": "0123456789",
"additional_email": "e.example#gmail.com",
"education": [
{
"endDate": "2020-06",
"startDate": "2015-09",
"profession": "Computer Since",
"university": "State University",
"educationType": 99
}
],
"language": [
{
"id": 102,
"level": 106
},
{
"id": 103,
"level": 106
},
{
"id": 104,
"level": 107
}
],
"computer_skills": [
{
"level": 106,
"skill": "php"
},
{
"level": 107,
"skill": "java"
}
],
"family_composition": [
{
"name": "Jhon",
"level": 126,
"surname": "Snow",
"birthday": "1992-02-08",
"fatherName": "Ned"
},
{
"name": "Jhon",
"level": 126,
"surname": "Snow",
"birthday": "1992-05-18",
"fatherName": "Ned"
}
],
"experience": [
{
"job": 128,
"time": 22,
"level": 8,
"salary": 2200,
"jobSign": 128,
"jobStatus": 267,
"startDate": "2012-12-12",
"orderNumber": "123dsa",
"jobSituation": 273,
"additionalSalary": 800
}
],
"reproach": [
{
"doc": 228,
"date": "2011-11-11",
"note": "Some reason",
"level": 225,
"number": "123dsa",
"reason": "islemir",
"article": 233
}
],
"additional_information": "All is work",
"citizen": {
"id": 5,
"name": "United States"
},
"cities": {
"id": 21,
"name": "New York"
},
"countries": {
"id": 89,
"name": "Unated States"
},
"gender": {
"id": 1,
"name": "Man"
},
"marital": {
"id": 4,
"name": "Single"
},
"work": {
"id": 269,
"name": "Able"
},
"party": {
"id": 10,
"name": "Digər"
},
"military": {
"id": 121,
"name": "OK"
},
"institution": null
}
}
I want to search like this:
WHERE education.'$[*].profession' Like %Computer%
But this syntax is not working. Thank you for replying. I developed my project in Laravel 5.7 if this is help for any suggestion. I don't use JSON_SEARCH() because this function returns the key but I need to return rows for my search query.

If you are on MySQL 5.7, this should work
.....WHERE JSON_EXTRACT(education , "$.profession") Like '%Computer%';

I found this solution and it worked for me:
UPPER(education->"$[*].profession") LIKE UPPER("% Computer %")
For Laravel syntax I write the PHP code like this:
$query=$query->whereRaw('UPPER(education->"$[*].profession") LIKE UPPER("%' . $profession . '%")');

Tested with MySQL 5.7:
SELECT 'found it!' FROM whatever_your_table_name_is
WHERE whatever_your_column_name_is->>'$.user_profile.education[*].profession'
LIKE '%Computer%';
Output, showing the WHERE clause matches the document:
+-----------+
| found it! |
+-----------+
| found it! |
+-----------+
As with most other questions about JSON in MySQL, I think you would be better off storing data in normalized tables.

Related

Skipping an object if one of the attributes is null in laravel

Task: you need to get a students object, with the conditions "group_id*" and "specialty_*id", where all these 3 tables are connected. It is also necessary to paginate only those objects of students for whom a group and specialty was found.
My problem: if the group or specialty data is not found, then the object is still added with the "group: null" attribute to pagination, thereby failing to display the requested number of elements on the page. How can this problem be solved?
The code:
$students = Student::with([
"group" =>
function($query) {
$query->where("id", \request("group_id"));
},
"group.speciality" =>
function($query) {
$query->where("id", \request("speciality_id"));
},
])->paginate(\request("page_size") ? : 10)->toArray();
return response()->json($students);
Preview: when I make a request with parameters
group_id = 2
speciality_id = 2
page_size = 2
The following object is returned:
{
"current_page": 1,
"data": [
{
"id": 1,
"receipt_date": "2010-11-02",
"user": {
"id": 1,
"login": "ykirillova#gmail.com",
"phone": "+7 (922) 472-9240",
"role": "user",
"passport": {
"series": 1762,
"number": 384282,
"date_of_issue": "1991-11-27",
"issued": "magni",
"division_code": 3,
"scan": "*photo link*",
"secondname": "Kilikova",
"firstname": "Olga",
"thirdname": "Anisimova",
"birthday": "1973-05-13",
"sex": "W"
}
},
"group": {
"id": 2,
"group_code": "4433",
"speciality": {
"id": 2,
"specialty_title": "Programming in computer systems",
"faculty": "SPO IKTZI"
}
}
},
{
"id": 2,
"receipt_date": "1973-11-07",
"user": {
"id": 2,
"login": "marta.fedorov#dackov.net",
"phone": "+7 (922) 903-0339",
"role": "user",
"passport": {
"series": 8241,
"number": 419233,
"date_of_issue": "1980-06-05",
"issued": "quos",
"division_code": 33,
"scan": "*photo link*",
"secondname": "Efremov",
"firstname": "Boleslav",
"thirdname": "Kostin",
"birthday": "2009-04-03",
"sex": "W"
}
},
"group": null
}
],
"per_page": "2",
"total": 75
}
Whereas with group == null, only 2 objects with groups should be returned
{
"current_page": 1,
"data": [
{
"id": 1,
"receipt_date": "2010-11-02",
"user": {
"id": 1,
"login": "ykirillova#gmail.com",
"phone": "+7 (922) 472-9240",
"role": "user",
"passport": {
"series": 1762,
"number": 384282,
"date_of_issue": "1991-11-27",
"issued": "magni",
"division_code": 3,
"scan": "*photo link*",
"secondname": "Kilikova",
"firstname": "Olga",
"thirdname": "Anisimova",
"birthday": "1973-05-13",
"sex": "W"
}
},
"group": {
"id": 2,
"group_code": "4433",
"speciality": {
"id": 2,
"specialty_title": "Programming in computer systems",
"faculty": "SPO IKTZI"
}
}
},
{
"id": 5,
"receipt_date": "2002-07-05",
"user": {
"id": 5,
"login": "tester#mail.ru",
"phone": "+7 (800) 555-3535",
"role": "user",
"passport": {
"series": 5521,
"number": 866521,
"date_of_issue": "1980-06-05",
"issued": "quos",
"division_code": 33,
"scan": "*photo link*",
"secondname": "Pavlov",
"firstname": "Denis",
"thirdname": "Artemev",
"birthday": "2009-04-03",
"sex": "W"
}
},
"group": {
"id": 2,
"group_code": "4433",
"speciality": {
"id": 2,
"specialty_title": "Programming in computer systems",
"faculty": "SPO IKTZI"
}
}
}
],
"per_page": "2",
"total": 75
}
The data presented is irrelevant as it is randomly generated.
Removing data elements in a loop with group equal to null does not solve the problem, since pagination gets lost.

How to merge two Arrays/Json with special conditions - PHP

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.

Is it possible extract and merge a single json object from multiple json files?

Sorry for the confusing title. I am having a bit of an issue here merging some JSON files. I need to merge all the products into one array in a separate file.
I have a directory full of same structured json files. I am using glob to select all files and decode->append-->encode json files into one large file.
Here is my code:
<?php
$files = glob("*.json");
$newDataArray = [];
foreach($files as $file){
$thisData = file_get_contents($file);
$thisDataArray = json_decode($thisData);
$newDataArray[] = $thisDataArray;
}
$newDataJSON = json_encode($newDataArray);
file_put_contents("merged.json",$newDataJSON);
?>
Now, the above code seems to work great but I only want to extract all the products.
Quick example of what I need to achieve.
File1.json
{
"status": true,
"user": {
"username": "sally",
"avatar": "/images/default-avatar.png",
"products": [
{
"id": "35vR4hr",
"title": "Picture 1",
"image": null,
"quantity": {
"min": 1,
"max": 1
},
"price": 2,
"currency": "CAD",
"stock_warning": 1,
"type": "service",
"stock": 9223372036854776000
},
{
"id": "na1Id4t",
"title": "Picture 2",
"image": null,
"quantity": {
"min": 1,
"max": 1
},
"price": 0.75,
"currency": "CAD",
"stock_warning": 3,
"type": "service",
"stock": 9223372036854776000
}
]
}
}
File2.json
{
"status": true,
"user": {
"username": "Jessica",
"avatar": "/images/default-avatar.png",
"products": [
{
"id": "wjiefi94",
"title": "Picture 3",
"image": null,
"quantity": {
"min": 1,
"max": 1
},
"price": 2,
"currency": "CAD",
"stock_warning": 1,
"type": "service",
"stock": 9223372036854776000
},
{
"id": "n34idwi",
"title": "Picture 4",
"image": null,
"quantity": {
"min": 1,
"max": 1
},
"price": 0.75,
"currency": "CAD",
"stock_warning": 3,
"type": "service",
"stock": 9223372036854776000
}
]
}
}
I want the data to be merged like:
merged.json
{
"products": [
{
"id": "wjiefi94",
"title": "Picture 1",
"image": null,
"quantity": {
"min": 1,
"max": 1
},
"price": 2,
"currency": "CAD",
"stock_warning": 1,
"type": "service",
"stock": 9223372036854776000
},
{
"id": "n34idwi",
"title": "Picture 2",
"image": null,
"quantity": {
"min": 1,
"max": 1
},
"price": 0.75,
"currency": "CAD",
"stock_warning": 3,
"type": "service",
"stock": 9223372036854776000
},
{
"id": "n34idwi",
"title": "Picture 3",
"image": null,
"quantity": {
"min": 1,
"max": 1
},
"price": 0.75,
"currency": "CAD",
"stock_warning": 3,
"type": "service",
"stock": 9223372036854776000
},
{
"id": "n34idwi",
"title": "Picture 4",
"image": null,
"quantity": {
"min": 1,
"max": 1
},
"price": 0.75,
"currency": "CAD",
"stock_warning": 3,
"type": "service",
"stock": 9223372036854776000
}
]
}
I hope this makes sense. I feel like I have hit a dead end here. Any help is greatly appreciated.
would it be possible for you to call an external tool like "jq"? handling json (just like csv), especially with many files, is not something you should be doing manually.
btw, your example does not have commas between products 2 and 3 and 3 and 4.
Your new code on line 5 should probably read like this with the array brackets? Otherwise you are overwriting the contents of the last files:
$thisDataArray[] = json_decode($thisData);
And why are you merging products from Sally and Jessica into the same user? Maybe you can just extract all the products objects into one file?
More of a code review than an answer, hope it helps ;)

Count Json elements wiht PHP

So I have
{
"members": [
{
"username": "John",
"status": "offline",
"avatar_url": "...",
"id": "830232882252102064"
},
{
"username": "Momo",
"status": "online",
"avatar_url": "...",
"id": "259137993351102464"
}
]
}
How do I count (in php) how many users are offline and how many users are online and return them into a value like $memonline and $memoffline.
Here in below code we are getting values of all statuses and then we are counting values of all statuses.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string='{
"channels": [
{
"position": 13,
"id": "304700935878213642",
"name": "20KBPS"
},
{
"position": 12,
"id": "304700895978061835",
"name": "50KBPS"
},
{
"position": 11,
"id": "304701193261809672",
"name": "70KBPS"
},
{
"position": 10,
"id": "304701326288224256",
"name": "90KBPS"
},
{
"position": 1,
"id": "304699877621891072",
"name": "=================="
},
{
"position": 9,
"id": "304700570592346114",
"name": "=================="
},
{
"position": 4,
"id": "304701407221514240",
"name": "=================="
},
{
"position": 14,
"id": "304700808883339264",
"name": "=================="
},
{
"position": 2,
"id": "304700525939523584",
"name": "Channel 1"
},
{
"position": 3,
"id": "304700547426942976",
"name": "Channel 2"
},
{
"position": 0,
"id": "304692483973971990",
"name": "General Channel"
},
{
"position": 6,
"id": "304701637446991873",
"name": "Private/2"
},
{
"position": 5,
"id": "304701480605319178",
"name": "Private/2"
},
{
"position": 7,
"id": "304701680010788866",
"name": "Private/3"
},
{
"position": 8,
"id": "304701738999611394",
"name": "Private/3"
},
{
"position": 15,
"id": "304700776691793921",
"name": "Trash Bin"
}
],
"instant_invite": null,
"id": "304692483973971989",
"members": [
{
"username": "Dyno",
"status": "online",
"bot": true,
"game": {
"name": "dynobot.net | ?help"
},
"avatar_url": "https://cdn.discordapp.com/avatars/155149108183695360/5aeb68c29b56b3d92eddb6f46df5051c.jpg",
"avatar": "5aeb68c29b56b3d92eddb6f46df5051c",
"discriminator": "3861",
"id": "155149108183695360"
},
{
"username": "Momo",
"status": "online",
"bot": true,
"game": {
"name": "$help | $info"
},
"avatar_url": "https://cdn.discordapp.com/avatars/259137993351102464/a3005ab7aff3eb829fecf375931a76f1.jpg",
"avatar": "a3005ab7aff3eb829fecf375931a76f1",
"discriminator": "4649",
"id": "259137993351102464"
},
{
"username": "Ronny Dark",
"status": "online",
"avatar_url": "https://cdn.discordapp.com/avatars/152855546574143492/6d9b98972ca6f0308be4dd2aec5aaca3.jpg",
"avatar": "6d9b98972ca6f0308be4dd2aec5aaca3",
"discriminator": "1480",
"id": "152855546574143492"
}
],
"name": "Server name"
}';
$array=json_decode($string,true);
$result=array_column($array["members"],"status");
$members=array_count_values($result);
echo isset($members["offline"]) ? $members["offline"] : 0;

Parsing Rome2Rio json file with PHP

I am trying to extract a segment from the json file of Rome2Rio API with PHP but I cant get an output.
The json file from rome2rio:
{
"serveTime": 1,
"places": [
{ "kind": "town", "name": "Kozani", "longName": "Kozani, Greece", "pos": "40.29892,21.7972", "countryCode": "GR", "regionCode": "ESYE13" },
{ "kind": "city", "name": "Thessaloniki", "longName": "Thessaloniki, Greece", "pos": "40.64032,22.93527", "countryCode": "GR", "regionCode": "ESYE12" }
],
"airports": [],
"airlines": [],
"aircrafts": [],
"agencies": [{
"code": "KTEL",
"name": "KTEL",
"url": "http://www.ktelbus.com/?module=default\u0026pages_id=15\u0026lang=en",
"iconPath": "/logos/Trains/KTELgr.png",
"iconSize": "27,23",
"iconOffset": "0,0"
}
],
"routes": [
{ "name": "Bus", "distance": 121.04, "duration": 120, "totalTransferDuration": 0, "indicativePrice": { "price": 9, "currency": "EUR", "isFreeTransfer": 0 },
"stops": [
{ "name": "Kozani", "pos": "40.30032,21.79763", "kind": "station", "countryCode": "GR", "timeZone": "Europe/Athens" },
{ "name": "Thessaloniki", "pos": "40.6545,22.90233", "kind": "station", "countryCode": "GR", "timeZone": "Europe/Athens" }
]
The PHP code I wrote is:
$json_rome2rio = file_get_contents("http://free.rome2rio.com/api/1.2/json/Search?key=&oName=kozani&dName=thessaloniki");
$parsed_json_r = json_decode($json_rome2rio);
echo $parsed_json_r->agencies->name;
The agencies property contains an array of agencies (note the square brackets). To access the name as you're after, you can do the following:
echo $parsed_json_r->agencies[0]->name;
This assumes that at least one agency is returned and that the agency you are after is the first one if more than one is returned.

Categories