I've tried to switch the datasource of the Kendo UI Gantt example inside PHP. I have mapped the schema with what is being returned, but I just get a blank gantt chart with one heading - "undefined".
{
"1": {
"id": "1",
"orderId": "1",
"title": "TESTER1",
"start": "\/new Date('2016-01-01 09:00:00')\/",
"end": "\/new Date('2016-02-01 00:00:00')\/",
"project": "1",
"client": "4218",
"parent": "0",
"percentComplete": "10.11"
},
"2": {
"id": "2",
"orderId": "2",
"title": "TESTER2",
"start": "\/new Date('2016-01-03 09:00:00')\/",
"end": "\/new Date('2016-02-01 00:00:00')\/",
"project": "1",
"client": "4218",
"parent": "0",
"percentComplete": "50.00"
}
}
Above is the JSON being sent back to Kendo, but it doesn't render.
Found the solution:
I type casted the integers, set parents to null rather than zero (0) and converted dates into milliseconds in the PHP layer before putting through to Kendo. I also removed the keys which resulted in the below JSON to be created. This solved my rendering problem.
[{
"id": 1,
"orderId": 1,
"title": "TESTER1",
"start": "\/Date(1463126400000)\/",
"end": "\/Date(1463958000000)\/",
"project": 1,
"client": 4218,
"parent": null,
"percentComplete": 10
}, {
"id": 2,
"orderId": 2,
"title": "TESTER2",
"start": "\/Date(1463990400000)\/",
"end": "\/Date(1464130800000)\/",
"project": 1,
"client": 4218,
"parent": null,
"percentComplete": 50
}]
Related
I have been on a little project of mine now i want to find imdb_id using tmdb_id so for that I have been trying to use the API.
https://api.themoviedb.org/3/tv/67026?api_key=myapikey&append_to_response=external_ids
which brings up the results like this
{
"backdrop_path": "/hcFbIbDzsB9aSSw9VkSGFEl5sGO.jpg",
"created_by": [{
"id": 230174,
"credit_id": "577eb8e5c3a368694a0027ac",
"name": "David Guggenheim",
"gender": 2,
"profile_path": "/hqSydaadHO6EsBIn3BQEzzfxNUY.jpg"
}],
"episode_run_time": [42],
"first_air_date": "2016-09-21",
"genres": [{
"id": 18,
"name": "Drama"
}, {
"id": 10768,
"name": "War & Politics"
}],
"homepage": "https://www.netflix.com/title/80113647",
"id": 67026,
"in_production": false,
"languages": ["en"],
"last_air_date": "2019-06-07",
"last_episode_to_air": {
"air_date": "2019-06-07",
"episode_number": 10,
"id": 1809432,
"name": "#truthorconsequences",
"overview": "On election day, Kirkman turns to his therapist to assuage his conscience about the events -- and his own decisions -- of the momentous prior 36 hours.",
"production_code": "",
"season_number": 3,
"show_id": 67026,
"still_path": "/cpy3uV100RyZuvJN535JLTrj4Nz.jpg",
"vote_average": 7.0,
"vote_count": 1
},
"name": "Designated Survivor",
"next_episode_to_air": null,
"networks": [{
"name": "ABC",
"id": 2,
"logo_path": "/ndAvF4JLsliGreX87jAc9GdjmJY.png",
"origin_country": "US"
}, {
"name": "Netflix",
"id": 213,
"logo_path": "/wwemzKWzjKYJFfCeiB57q3r4Bcm.png",
"origin_country": ""
}],
"number_of_episodes": 53,
"number_of_seasons": 3,
"origin_country": ["US"],
"original_language": "en",
"original_name": "Designated Survivor",
"overview": "Tom Kirkman, a low-level cabinet member is suddenly appointed President of the United States after a catastrophic attack during the State of the Union kills everyone above him in the Presidential line of succession.",
"popularity": 30.031,
"poster_path": "/5R125JAIh1N38pzHp2dRsBpOVNY.jpg",
"production_companies": [{
"id": 28788,
"logo_path": null,
"name": "Genre Films",
"origin_country": "US"
}, {
"id": 19366,
"logo_path": "/vOH8dyQhLK01pg5fYkgiS31jlFm.png",
"name": "ABC Studios",
"origin_country": "US"
}, {
"id": 78984,
"logo_path": null,
"name": "Entertainment 360",
"origin_country": "US"
}],
"seasons": [{
"air_date": "2016-09-20",
"episode_count": 21,
"id": 78328,
"name": "Season 1",
"overview": "Tom Kirkman, a low-level cabinet member is suddenly appointed President of the United States after a catastrophic attack during the State of the Union kills everyone above him in the Presidential line of succession.",
"poster_path": "/1QHlD6z9FnXuuTDVLJnjrtLfVyq.jpg",
"season_number": 1
}, {
"air_date": "2017-09-27",
"episode_count": 22,
"id": 91130,
"name": "Season 2",
"overview": "",
"poster_path": "/z4hdj8cYyqCO9lVBOGm6YZsnMho.jpg",
"season_number": 2
}, {
"air_date": "2019-06-07",
"episode_count": 10,
"id": 122914,
"name": "Season 3",
"overview": "",
"poster_path": "/wn310FWQhjjpHbqsMRBcXr28EHc.jpg",
"season_number": 3
}],
"status": "Canceled",
"type": "Scripted",
"vote_average": 7.2,
"vote_count": 408,
"external_ids": {
"imdb_id": "tt5296406",
"freebase_mid": null,
"freebase_id": null,
"tvdb_id": 311876,
"tvrage_id": 51115,
"facebook_id": "DesignatedSurvivor",
"instagram_id": "designatedsurvivor",
"twitter_id": "DesignatedNFLX"
}
}
So now if I want to get just the IMDb_ID form the external_ids then what code should i use in PHP and store it in a variable.
Thank you very much in advance.
use the json_decode method to treat JSON like a PHP object
$data = 'your JSON object';//raw data as string in $data
$decoded = json_decode($data);//decoded data as PHP object
echo $decoded->external_ids->imdb_id; //access any property you like
hope this helps!!!
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 ;)
I am trying to grab data by using JSON in ionWordpress (ionic framework plugin) for wordpress.
I'm using the JSON API. Here's the data from my JSON.
{
"status": "ok",
"count": 6,
"count_total": 45,
"pages": 8,
"posts": [
{
"id": 1874,
"type": "event",
"slug": "stesets",
"url": "http://www.web.com/page",
"status": "publish",
"title": "setst",
"title_plain": "tests",
"content": "testsetstst",
"excerpt": "testsetststtes",
"date": "2017-11-27 17:20:17",
"modified": "2017-11-27 17:20:17",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "test",
"name": "test",
"first_name": "",
"last_name": "",
"nickname": "test",
"url": "",
"description": ""
},
"comments": [],
"attachments": [],
"comment_count": 0,
"comment_status": "closed",
"thumbnail": "http://www.website.com/wp-content/uploads/2017/11/image.jpg",
My problem is when I tried to grab the data, it includes the [] as well. For eg.
{{post.thumbnail}}
This would show ["http://www.website.com/wp-content/uploads/2017/11/image.jpg"]
but I just want the link itself without the [" "]
How do I do this?
I used:
{{post.thumbnail[0]}}
as mentioned in comments by Diluk Angelo.
Could someone help me think of a smart algorithm?
Problem
JSON data looks kinda like this (can't give the real data :/):
{
"$id": "1",
"Start": 1000,
"End": 1200,
"Account": {
"$id": "2",
"id": "af51f511-e851-4d01-808b-2fa3c070d3ad",
"aLotMoreInfo": "cool",
"Parents": {
"$id": "3",
"id": "73d7a1f1-82e8-4a7e-bccb-838a251e1a38",
"aLotMoreInfo": "notcool",
"Parents": null,
"Children": {
"ref": 2
}
},
"Children": null
}
}
Anyways the problem now is that it contains ref with a value behind it. The above JSON should be like this:
{
"$id": "1",
"Start": 1000,
"End": 1200,
"Account": {
"$id": "2",
"id": "af51f511-e851-4d01-808b-2fa3c070d3ad",
"aLotMoreInfo": "cool",
"Parents": {
"$id": "3",
"id": "73d7a1f1-82e8-4a7e-bccb-838a251e1a38",
"aLotMoreInfo": "notcool",
"Parents": null,
"Children": {
"$id": "2",
"id": "af51f511-e851-4d01-808b-2fa3c070d3ad",
"aLotMoreInfo": "cool",
"Parents": {
"$id": "3",
"id": "73d7a1f1-82e8-4a7e-bccb-838a251e1a38",
"aLotMoreInfo": "notcool",
"Parents": null,
"Children": null
},
"Children": null
},
"Children": null
}
}
}
I need to know two things:
how to easily get the JSON data which has been stated before
how do I copy it but set the value Children to null every single
time?
I hope someone know a good way how this can be done, because I can't really think of a clever way...
I have this JSON string:
{
"product": [
{
"id": "1",
"title": "producta",
"size": "50",
"weight": "1000",
"price": "30",
"quantity": "100",
"cartID": "1"
},
{
"id": "1",
"title": "producta",
"size": "50",
"weight": "1000",
"price": "30",
"quantity": "100",
"cartID": "2"
}
]
}
When I use the PHP function json_decode($products, true), and then I re-encode it using json_encode($products), the string becomes this:
{
"product": {
"1": {
"id": "2",
"title": "producta",
"size": "50",
"weight": "1000",
"price": "30",
"quantity": "100",
"cartID": "2"
},
"2": {
"id": "1",
"title": "producta",
"size": "50",
"weight": "1000",
"price": "30",
"quantity": "100",
"cartID": "3"
}
}
}
After decoding and re-encoding, it adds a key to every "product"
Is there a way around this?
Posting this here since it'd be too ugly in a comment, but this is what I get on PHP 5.3.6 after doing a echo json_encode(json_decode('...your json...', true));:
{"product": [
{"id":"1","title":"producta","size":"50","weight":"1000","price":"30","quantity":"100","cartID":"1"},
{"id":"1","title":"producta","size":"50","weight":"1000","price":"30","quantity":"100","cartID":"2"}
]}
Note the lack of extra keys. Are you doing any manipulations on the decoed array before re-encoding?
You are probably using mysqli_fetch_array instead of mysqli_fetch_assoc. Meaning u are json decoding a key arreay instead of an associative array