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.
Related
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!
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
Part of the script says from field to field which is to do with this. Here's my JSON pulling info from my database, value 1 is amount, value 2 is time
[{
"date": "2014-12-09",
"value1": 367,
"value2": 03:06:00
}, {
"date": "2014-12-09",
"value1": 367,
"value2": 03:06:00
}]
That's my data, this is part of the script that wants me to map it? :S
dataSet.fieldMappings = [{
fromField: "valu1",
toField: "value2"
}, {
fromField: "volume",
toField: "volume"
}];
I only have 3 fields don't I? Sorry for a vague question, I just don't understand.
The fieldMapping maps the JSON-fields with the valueField specified in the graph-properties.
I guess what you want to do is something like this:
[{
"date": "2014-12-09 03:06:00",
"value": 367
}, {
"date": "2014-12-09 03:07:00",
"value": 673
}]
Note that i put the time into the date-field and changed the second date and value to be different from the first. (I think two similar datapoints won't work)
For this dataset your mapping should look like this:
dataSet.fieldMappings = [{
fromField: "value", // this is the JSON-field
toField: "valueMapped" // the graph will search for this field - see below
}];
To find the data, the graph needs to know the mapped field:
"graphs": [{
"valueField": "valueMapped"
}];
Now one last step for the correct use of the "date"-field:
"dataDateFormat": "YYYY-MM-DD JJ:NN:SS",
"categoryField": "date"
So they have this JSON and i need specific items to get e.g. promotions:
{
"currency": "USD",
"code": 200,
"sales_list": [
{
"date": "2012-05-21",
"country": "JP",
units:{
app: {
downloads: 10,
updates: 1,
},
iap: {
sales: 3,
refunds: 5,
promotions:1
},
},
revenue: {
app: {
downloads “100.98”,
refunds: “-10.00”,
promotions: “1.00”
},
iap: {
sales: “30.00”,
refunds: “-1.00”,
},
ad: "1000.00"
},
}],
“iap_sales”:[
{
"date": "2012-05-21",
"country": "JP",
“iap”: “com.iap1”,
units:{
sales: 3,
refunds: 5,
},
revenue: {
sales: "30.09",
refunds: "1.21",
},
}],
"page_index": 1,
}
How do i get the to the promotions?
I tried to use this:
$json_data = connect(t_url);
$data = json_decode($json_data);
return $data->sales_list[0]->units[0]->app[0]->promotions;
but is it even effective/ efficient?
Also im having a hard time getting data from appannie. i always get authorization problem or bad request. If bad request it means im in though right?
I use the terminal to test curl and my url like:
curl --basic --user email : password "http:/www.appannie.com/v1/accounts/acntnum/..."
they suggested this since they have authentication. There are time i get bad request but i think im in unlike unauthorized access error.
They also suggest other forms like the authethication should be basic+base 64 of ur eadd and password. But I don't know where to use it.
Any suggestions to convert this into php curl request?
I've search Stack Overflow and Google for an answer but not luck. I'm trying to get the value of each of the locale with php in the following sample (facebook graph api). Any help would be appreciated.
"data": [
{
"id": "123456789/insights/page_fans_locale/lifetime",
"name": "page_fans_locale",
"period": "lifetime",
"values": [
{
"value": {
"en_US": 33975,
"fr_CA": 6906,
"fr_FR": 6105,
"en_GB": 5647
},
"end_time": "2012-03-14T07:00:00+0000"
},
{
"value": {
"en_US": 33992,
"fr_CA": 6906,
"fr_FR": 6107,
"en_GB": 5648
},
"end_time": "2012-03-15T07:00:00+0000"
},
}
First, this fragment is broken. data is a array not a JavaScript object.
Take the correct fragment and analyze data correctly. The best option is:
json_decode();
Here is how to use it.