search through JSON Object for specific key - php

I'm getting a JSON response from an API provider which I can't change, and there are lots of nested objects in the response and then there is an object which I need, below is just the example of response actual response has a lot of nested objects and then the match object,
I can easily access the match object but in some case, the API provider add some more object above it as shown in the 2nd response where he added the group and then there is match object which I need,
still by using some if, else we can get through it.
but are there any methods or ways available that can directly give me access to the match object or can find the match object?
I'm using laravel 5.8
and tried some of the collection methods none of them work on objects.
in actual response, I have to go 7 steps nested.
//1nd API response
[{
"version": "2.258.2",
"sport": "badminton",
"lang": "en",
"generated_utc": "2021-07-28T13:58:16+00:00",
"method": {
"method_id": "258",
"name": "get_matches"
"match" : {
score_a:0,
score_b:1
}
}
}]
//2nd API response
[{
"version": "2.258.2",
"sport": "badminton",
"lang": "en",
"generated_utc": "2021-07-28T13:58:16+00:00",
"method": {
"method_id": "258",
"name": "get_matches"
"group":{
"match" : {
score_a:0,
score_b:1
}
}
}
}]
Thank You!

Related

Advice on extracting data from JSON stream

I have a JSON data stream which contains a bunch of JSON objects prefixed with the 'Data: ' label. What is the simplest way to make this valid JSON so I can utilise the data stream in an application? The URL given is constantly updating so it has no end.
Short snippet of some of the streamed data:
data: {"value":12,"id":"window_doneTodayCount_1","updatedAt":1646316313574}
data: {"value":"4:30","id":"window_averageTime_1","updatedAt":1646316313596}
data: {"value":"Tue 11:05","id":"window_lastUpdatedTime","updatedAt":1646316313746}
I am looking to convert this dynamically to something like the below, which would be valid and I can use within an app:
[{
"value": 12,
"id": "window_doneTodayCount_1",
"updatedAt": 1646316313574
},
{
"value": "4:30",
"id": "window_averageTime_1",
"updatedAt": 1646316313596
}, {
"value": "Tue 11:05",
"id": "window_lastUpdatedTime",
"updatedAt": 1646316313746
}
]
Not even sure if this is possible as I have never seen a file like this.
Thanks.

How to access json data in querybuilder parser for laravel?

I am a novice developer and I am stuck here since last few days. I am using jQuery query builder parser for laravel by timgws and sending the form data by Ajax. I can get the query in the controller but I want to access the JSON data before making the query. I was following this reference but i can't access the data even after json_decode() with "true" argument. Here is the data I receive in the controller:
{
"condition": "AND",
"rules": [
{
"id": "diseases",
"field": "mesh_id",
"type": "string",
"input": "select",
"operator": "equal",
"value": "C535575"
}
],
"valid": true
}
How can I access this value/type/fields from this data?

I want a more consolidate json format

I am getting json array after getting applying query logic.
[
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-20"
},
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "CRP",
"booking_date": "2018-05-20"
},
{
"id": "4",
"diag_name": "Seepz Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-21"
}
]
But i want a more justified json array written below.
[
{
"diag_name": "LT Diagnostics",
"test_name": [
{
"id": "3",
"name" : "Alk PO4"
},
{
"id": "3",
"name" : "CRP"
}
],
"booking_date": "2018-05-20"
},
{
"diag_name": "Seepz Diagnostics",
"test_name": [
{
"id": "4",
"name" : "Alk PO4"
}
],
"booking_date": "2018-05-21"
},
]
I am not getting it,How to do in php. I want a more consolidate json format.
Have you tried changing your SQL query to group by diag_name and booking_date? That would be the first step I’d employ to get the outer data.
Formatting the data in the nested manner you’re after could be a function of whatever record serializer you’re using — does it support nested JSON as a return type, or only flat JSON as your example return value shows?
If the record set -> JSON serializer only ever returns flat data, the comments above are correct that you will have to write your own formatter to change the shape of the JSON yourself...
The accepted answer of this other question may be of help:
Create multi-level JSON with PHP and MySQL
I'm not a PHP guy but this is a typical scenario to use functional programming by means of the monad Map.
Looking online I've found this article that could help you.
Changing datasource output is not always (seldom indeed) a viable option.
Enjoy coding

JMSSerializerBundle and Symfony 2 - Output Doctrine/ODM to JSON file

I am trying to retrieve all records and display them in a JSON file.
My current function retrieves all Events that belong to a specific user.
/**
* create json files from doctrine/mongo
* #Route("/createjson", name="createjson")
*/
public function createJson()
{
// check user authentication
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$dm = $this->get('doctrine_mongodb')->getManager();
$repository = $dm->getRepository('AppBundle:Event');
$events = $repository->findBy(array('user' => $this->getUser()));
$serializer = SerializerBuilder::create()->build();
$result = $serializer->deserialize($events, 'AppBundle\Document\Event', 'json');
var_dump($result);
exit;
}
This is not working because some of the elements passed into the serializer are of an array format. Error I am getting.
Warning: json_decode() expects parameter 1 to be string, array given
500 Internal Server Error - ContextErrorException
However if I use the inbuilt Symfony Serializer it works fine:
$serializer = $this->container->get('serializer');
$reports = $serializer->serialize($events, 'json');
return new Response($reports);
However the JSON to be produced will be different to my Document/Entity hence why I want/need to use the JMSSerializerBundle.
For example, a record looks like this:
[{
"id": "572041b3288b560e5e00451c",
"name": "Test",
"date": "2016-04-27T05:25:00+1000",
"enddate": "2016-04-30T11:55:00+1000",
"location": {
"name": "Sydney, NSW"
},
"key": {
"id": "1g43g34g34g23f32g32G32gGSDF"
},
"user": {
"id": "57203174288b560e5e0044da"
}, ...
}]
But I only want to display (output) to JSON
[{
"id": "572041b3288b560e5e00451c",
"name": "Test",
"date": "2016-04-27T05:25:00+1000",
"location": "Sydney, NSW"
}]
How would I go about doing this? There is not much documentation on JMSSerializerBundle online.
Edit: I should mention that the database collection I am querying has a relation to the User collection which is managed by FOSUserBundle. I'm not sure if this has any relation to my problem however
You should look at the documentation of the bundle, may be you will find more information
http://jmsyst.com/bundles/JMSSerializerBundle
http://jmsyst.com/libs/serializer/master/usage
You seems to use the wrong function of the serializer. In your case, you seems to need to get a json from your user object, so you need to use
$serializer = SerializerBuilder::create()->build();
$result = $serializer->serialize($events, 'AppBundle\Document\Event', 'json');
serialize($object):string : get a string from an object
deserialize($string):object : get an object from a representation of an object (json, xml...).

Retrieving tasks within teams in PHP

I am trying to develop a web application that can fetch data from Asana and generate custom spreadsheet reports. This wrapper class was very helpful in making things simple.
However, I am having a hard time in writing code that gets me the team/s that a particular task belongs to. Even when I export data as JSON through Asana's web application the 'teams' find no mention. From what I understand, Asana itself does not provide an association between teams and tasks. Please correct me if I am wrong.
But if I am right at my conclusion, is there a workaround I could use? Teams are an important part of my data rendering and I need them to be mapped correctly in my reports that I am trying to generate from Asana. The report I want to generate would be hierarchical in nature.
Organisation
Team
Projects
Tasks
Subtask
Can I do something to achieve this hierarchy? The only place I get stuck is getting the projects under a particular team.
Glad to hear that you found that wrapper useful. We will be releasing a PHP Library ourselves soon that you may be interested in. Stay tuned!
Below is some pseudo-code to derive the hierarchy you are looking for, I think. Let me know if it helps.
GET /workspaces
{
"data": [
{
"id": 1234,
"name": "Startup Inc"
}
]
}
GET /workspaces/1234
{
"data": {
"id": 1234,
"name": "Startup Inc",
"is_organization": true,
...
}
}
Because is_corganization is true, we can then continue...
GET /organizations/organization-id/teams
{
"data": [
{
"id": 9876,
"name": "Ninja Team"
}
]
}
GET /teams/9876/projects
{
"data": [
{
"id": 5678,
"name": "Stealth Project"
}
]
}
GET /projects/5678/tasks
{
"data": [
{
"id": 8675309,
"name": "Top secret video"
}
]
}
GET /tasks/8675309
{
"data": {
"id": 8675309,
"created_at": "2015-03-25T17:28:59.255Z",
"modified_at": "2015-05-15T03:13:28.754Z",
"name": "Top secret video",
"notes": "https://www.youtube.com/watch?v=6WTdTwcmxyo",
"completed": false,
... # All the task data
]
}
}

Categories