I am trying to return results of a specific table in Laravel while hiding a specific attribute from the returned SQL results.
Part of the JSON response is as shown
...
"custom_filters": [
{
"question_type": "single",
"question": "How long have you been an influencer",
"answer": 1
},
{
"question_type": "choice",
"question": "Have you ever went viral?",
"choices": [
"yes",
"no"
],
"answer": "yes"
},
{
"question_type": "single",
"question": "How many followers do you have on Instagram",
"answer": 12000
}
],
...
Expected Output
...
"custom_filters": [
{
"question_type": "single",
"question": "How long have you been an influencer",
},
{
"question_type": "choice",
"question": "Have you ever went viral?",
"choices": [
"yes",
"no"
]
},
{
"question_type": "single",
"question": "How many followers do you have on Instagram"
}
],
...
I want the answer part of the JSON OBJECT to be hidden/removed in the final output as shown above. I know i could get the results from my Eloquent model and filter the results accordingly discarding the answer part. but I feel like there is a better way of doing this.
by the way here is the code for fetching the results
$posts = Posts::where('status', 'published')->orderBy('post_id', 'desc')
->paginate(30);
use this
Posts::where('status', 'published')->orderBy('post_id', 'desc')->whereJsonContains('json_data', [['custom_filters' => ['answer' => 'yes']]])->get()
Related
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
I'm currently storing user-generated surveys in JSON files, and am now converting these to a sql database. Regarding this portion of JSON:
"surveys": [
{
"surveyId": 1,
"name": "Landing Page Survey",
"active": true,
"panes": [
{
"type": "question",
"name": "Question",
"head": "Is there anything preventing you from signing up for a free 14-day trial?",
"response": "textbox",
"options": [
{
"data": "Time",
"target": "Response 1",
"placeholder": "",
"list": "f7b3cdeed8"
},
{
"data": "Money",
"target": "Response 1",
"placeholder": "",
"list": "local"
},
{
"data": "I'm not interested",
"target": "Thanks",
"placeholder": "",
"list": "local"
}
],
"button": "Send"
},
{
"type": "response",
"name": "Response 1",
"head": "Thanks for your interest in our product. Enter your email address to have a team member follow-up with you.",
"response": "email",
"options": [
{
"data": "data",
"target": "Thanks",
"placeholder": "Your email",
"list": "f7b3cdeed8"
}
],
"button": "Submit"
},
{
"type": "thanks",
"name": "Thanks",
"head": "Thanks for your feedback!",
"response": "multichoice",
"options": [
{
"data": "data",
"target": "",
"placeholder": "put response here",
"list": "local"
}
],
"button": "Button text"
}
]
The JSON isn't that important, just posting so you can see the idea.. surveys.panes are the questions of the survey. Each survey could have an infinite number of questions in it, so I'm struggling with how to store those.
Originally I was thinking of having a surveys table with columns for the questions, 'question_1_type, question_1_text', etc.. This would work if each survey was limited to an amount of questions (10 for example, so I could create 10 sets of columns). This feels horribly wrong though.
Or is it more correct to create a questions table, and for each question in a survey create a row in the questions table. Link it to the survey table with an id, then when you want to output the survey JSON for an API or whatever, just do a bunch of joins across the survey and questions table.
Also, if a survey question is multiple choice, it could have an infinite number of response options also, so would you have to build a table for those as well? Then for each survey JSON build you'd have to join across the surveys, questions, and question_options table. That seems like a lot of overhead running all the joins..
But as I understand, it's incorrect to store anything but one value in a column (an array for example) as it defeats the relational idea of a sql db.
Very noob question.. I haven't quite wrapped my head around correct database design. Appreciate any help!
I am developing a system of forms built on Json data, in the form of form defitions, outputs, and then report templating. My app is based on Symfony2 and uses AWS S3 for the Json storage, but I should be able to move any generic PHP to that framework.
Basically, I need to be able to update the template json node which will then change the way that records are displayed in a page. The tough thing is that the output template will be multi level. Ie we may want to group on Name and then on Date at the next level down. The following is an example of how I might want it all to look.
QUESTION JSON - a definition of all questions
"questions": [
{
"id": 0,
"question": {
"question": "Name",
"type": "Textbox",
}
},
{
"id": 1,
"question": {
"question": "Email",
"type": "Textbox",
}
},
{
"id": 2,
"question": {
"question": "Date",
"type": "DateTime",
}
}
]
ANSWER ARRAY JSON - one for each form filled out
"Questions" : [
{
"answer" : "john smith"
"question" : 0
},
{
"answer" : "js#abc.com"
"question" : 1
},
{
"answer" : "01/01/2015"
"question" : 2
}
]
"Questions" : [
{
"answer" : "john smith"
"question" : 0
},
{
"answer" : "js_secondemail#abc.com"
"question" : 1
},
{
"answer" : "01/01/2015"
"question" : 2
}
]
OUTPUT TEMPLATE JSON - a definition of how we want the data to be displayed
"outputStructure": [{
"output": [{
"showReport": true,
"groupBy": 'Name'
}]
}]
And that should all end up with my php page displaying the following:
John Smith (link)
===js#abc.com.au
===js_secondemail#abc.com.au
If anyone can help it will be much appreciated!
Recently, our team is going to develop mobile(iphone, android platforms) applications for our existing website, let user can use the application to more easy to read our content via the application.
But our team have different views in JSON schema of the API return, below are the sample response.
Schema type 1:
{
"success": 1,
"response": {
"threads": [
{
"thread_id": 9999,
"title": "Topic haha",
"content": "blah blah blah",
"category": {
"category_id": 100,
"category_name": "Chat Room",
"category_permalink": "http://sample.com/category/100"
},
"user": {
"user_id": 1,
"name": "Hello World",
"email": "helloworld#hello.com",
"user_permalink": "http://sample.com/user/Hello_World"
},
"post_ts": "2012-12-01 18:16:00T0800"
},
{
"thread_id": 9998,
"title": "asdasdsad ",
"content": "dsfdsfdsfds dsfdsf ds",
"category": {
"category_id": 101,
"category_name": "Chat Room 2",
"category_permalink": "http://sample.com/category/101"
},
"user": {
"user_id": 2,
"name": "Hello baby",
"email": "hellobaby#hello.com",
"user_permalink": "http://sample.com/user/2"
},
"post_ts": "2012-12-01 18:15:00T0800"
}
]
}
}
Schema type 2:
{
"success": 1,
"response": {
"threads": [
{
"thread_id": 9999,
"title": "Topic haha",
"content": "blah blah blah",
"category": 100,
"user": 1,
"post_ts": "2012-12-01 18:16:00T0800"
},
{
"thread_id": 9998,
"title": "asdasdsad ",
"content": "dsfdsfdsfds dsfdsf ds",
"category": 101,
"user": 2,
"post_ts": "2012-12-01 18:15:00T0800"
}
],
"category": [
{
"category_id": 100,
"category_name": "Chat Room",
"category_permalink": "http://sample.com/category/100"
},
{
"category_id": 101,
"category_name": "Chat Room 2",
"category_permalink": "http://sample.com/category/101"
}
],
"user": [
{
"user_id": 1,
"name": "Hello World",
"email": "helloworld#hello.com",
"user_permalink": "http://sample.com/user/Hello_World"
},
{
"user_id": 2,
"name": "Hello baby",
"email": "hellobaby#hello.com",
"user_permalink": "http://sample.com/user/Hello_baby"
}
]
}
}
Some Developers claim that if using schema type 2,
can reduce data size if the category & user entities comes too much duplicated. it does really reduce at least 20~40% size of response plain text.
once if the data size come less, in parsing it to JSON object, the memory get less
categoey & user can be store in hash-map, easy to reuse
reduce the overhead on retrieving data
I have no idea on it if schema type 2 does really enhanced. Because I read so many API documentation, never seen this type of schema design. For me, it looks like a relational database. So I have few questions, because I have no experience on designing a web services API.
Does it against API design principle (Easy to read, Easy to use) ?
Does it really get faster and get less memory resource on parsing on IOS / Android platform?
Does it can reduce the overhead between client & server?
Thanks you.
When I do such an application for android, I parse JSON just one and put it in database. Later I'm using ContentProvider to access it. In Your case You could use 2nd schema but without user, category part. Use lazy loading instead but it will be good solution just in case categories and users repeat often.
first im sory my bad english
im getting page stats via graph api. (insights/page_fan_city)
my query and the facebook response is below.
{
"data": [
{
"id": "147197862002528/insights/page_fans_city/lifetime",
"name": "page_fans_city",
"period": "lifetime",
"values": [
{
"value": {
"istanbul": 20017,
"ankara": 9763,
"izmir": 7549,
"bursa": 3350,
"adana": 2949,
"antalya": 2687,
"konya": 2375,
"izmit": 2229,
"mersin": 1942,
"samsun": 1861,
"kayseri": 1444,
"maltepe": 1403,
"trabzon": 1327,
"manisa": 1163,
"eskisehir": 1040,
"baku": 1015,
"denizli": 986,
"mugla": 978,
"erzurum": 809
},
"end_time": "2011-12-17T08:00:00+0000"
}
],
"description": "Lifetime Aggregated Facebook location data, sorted by city, about the people who like your Page. (Total Count)"
}
],
"paging": {
"previous": "https://graph.facebook.com/147197862002528/insights/page_fans_city/lifetime/?access_token=HIDDEN&since=1323682381&until=1323941581",
"next": "https://graph.facebook.com/147197862002528/insights/page_fans_city/lifetime/?access_token=HIDDEN&since=1324200781&until=1324459981"
}
}
I get just this information. I want to get cities which is the country. How can i get the name of countries
Seems like an old question, but just to clear things up you could assign that JSON object to a string and recurse through it like so to get the countries:
$response = json_decode($json);
foreach((array)$response->data[0]->values[0]->value as $key => $val) {
print $key."\n";
}