I have this json data (actual data is a lot longer, that's why I need only 2)
[
{
"id": 1,
"user_id": 1,
"event_id": 1,
"creator_id": 1,
"event_name": "Poker",
"cruise_ship_name": "Royal Cruise",
},
{
"id": 2,
"user_id": 1,
"event_id": 2,
"creator_id": 1,
"event_name": "Ballroom",
"cruise_ship_name": "Celebrity Infinity",
},
{
"id": 3,
"user_id": 1,
"event_id": 3,
"creator_id": 1,
"event_name": "Tennis",
"cruise_ship_name": "Mediterranean",
}
]
I want to combine all data and get only specific fields (event_name and cruise_ship_name)
So in my final json format
it will be:
[
{
"event_name": "Mexican Fiesta",
"cruise_ship_name": "Celebrity Infinity",
}
]
I have been looking at this example:
#object.to_json {:include => [ :assocation_a, :assocation_b ]}
but not sure what :association_a and :association_b are.
Suppose you have an array of hashes:
events = [
{
"id": 1,
"user_id": 1,
"event_id": 1,
"creator_id": 1,
"event_name": "Poker",
"cruise_ship_name": "Royal Cruise",
},
...
]
You can iterate through each value in your hash, only keeping values of interest:
events.each do |event_hash|
event_hash.keep_if { |key, _| [:event_name, :cruise_ship_name].include?(key) }
end
puts events
The to_json method accept parameters which allow you include specific attributes:
#object.to_json(only: [:event_name, :cruise_ship_name])
The include: :assocation_a option to object, allowing the object association in the assocation_a model to be converted to JSON as well.
Related
I'm trying to create a structure for the following json object using swift decodable.
{
"template": [
{
"id": 8,
"question": "Favorite Color?",
"category": "Color",
"section": "Favorite Colors",
"is_active": 1,
},
[
{
"id": 14,
"question_id": 8,
"option_name": "Red",
"is_active": 1,
},
{
"id": 16,
"question_id": 8,
"option_name": "Orange",
"is_active": 1,
}
],
{
"id": 9,
"question": "What cars do you drive?",
"category": "Cars",
"section": "Favorite Cars",
"is_active": 1,
},
[
{
"id": 15,
"question_id": 9,
"option_name": "Toyota",
"is_active": 1,
},
{
"id": 18,
"question_id": 9,
"option_name": "Honda",
"is_active": 1,
},
{
"id": 19,
"question_id": 9,
"option_name": "BMW",
"is_active": 1,
}
]
]
}
I have some like:
public struct GameTemplate:Decodable {
question:String?
}
public struct Game:Decodable {
let template[GameTemplate]
}
For some reason when i tried to parse it doesn't work i get an error stating that struct is not a dictionary. I have tried casting the struct value but that didn't work either at this point just need to get a nice and clean json object after is decoded.
your JSON format is not consistent.
Just take the first category of color :
{
"template": [
{
"id": 8,
"question": "Favorite Color?",
"category": "Color",
"section": "Favorite Colors",
"is_active": 1,
},
[
{
"id": 14,
"question_id": 8,
"option_name": "Red",
"is_active": 1,
},
{
"id": 16,
"question_id": 8,
"option_name": "Orange",
"is_active": 1,
}
],
]
}
The template is an array having dictionary on 0 index and array on 1 index.
It is decodable in a different way but that's an extra effort.
If possible make the JSON data consistent and club categories in one index of array as :
{
"template": [
{
"id": 8,
"question": "Favorite Color?",
"category": "Color",
"section": "Favorite Colors",
"is_active": 1,
"subCategory": [
{
"id": 14,
"question_id": 8,
"option_name": "Red",
"is_active": 1,
},
{
"id": 16,
"question_id": 8,
"option_name": "Orange",
"is_active": 1,
}
]
}
]
}
and the same way club the different category of cars.
It will be easy for you to decode as:
public struct GameTemplate:Decodable {
question: String?
subCategory: [SubCategory]
}
public struct SubCategory:Decodable {
option_name: String?
}
public struct Game:Decodable {
let template: [GameTemplate]
}
Hope you get it what I am trying to explain.
Hi Im get json data with Alamofire and get like this:
{
"prices": [
{
"id": 1,
"value": 1.327,
"stationId": 24,
"type": 0,
"score": 5
},
{
"id": 2,
"value": 1.319,
"stationId": 25,
"type": 0,
"score": 4
},...],
"stations": [
{
"id": 24,
"name": "...",
"address": "...",
"brandId": 1,
"location": ".."
},
{
"id": 25,
"name": "..",
"address": "..",
"brandId": 1,
"location": ".."
},..],
"brands": [
{
"id": 6,
"name": "AGIP"
},
{
"id": 2,
"name": "EKO"
}, ...]
How can I get all data with "type": 0
And then when get all data with type compare id from prices stations and brands and put to array or dictionary
You could map your response using this pod AlamofireObjectMapper and then you can filter this object using for cycle.
I am trying to load data from redis db. I have a api only rails app and trying to render the json data as per requirement.
Currently I am able to get the data from redis in the following format.
[
{
"id": 1,
"name": "Stephenie Meyer",
"created_at": "2018-04-17T07:40:50.417Z",
"updated_at": "2018-04-17T07:40:50.417Z"
},
{
"id": 2,
"name": "V.C. Andrews",
"created_at": "2018-04-17T07:40:50.613Z",
"updated_at": "2018-04-17T07:40:50.613Z"
},
{
"id": 3,
"name": "Sophie Kinsella",
"created_at": "2018-04-17T07:40:50.646Z",
"updated_at": "2018-04-17T07:40:50.646Z"
}
]
How can convert this in a way such that the key value pairs of name,created and updated will be hash to a id key-value pair.
Into this
{"id": 1,
{
"name": "Stephenie Meyer",
"created_at": "2018-04-17T07:40:50.417Z",
"updated_at": "2018-04-17T07:40:50.417Z"
}
}
helper method for getting redis data.
def fetch_authors
authors = $redis.get('authors')
if authors.nil?
authors = Author.all.to_json
$redis.set("authors", authors).to_json
$redis.expire("authors", 5.hour.to_i)
end
JSON.load authors
end
And displaying on index page using
def index
#authors = fetch_authors
render json: #authors
end
The closest to what you want would probably be:
input = ...
input.map { |hash| [hash.delete(:id) || hash.delete('id'), hash] }.to_h
#⇒ {{1=>{:name=>...},
# {2=>{:name=>...},
# {3=>{:name=>...}}
Not exactly what you want because that's not correct syntax but you can achieve something similar with group_by
arr = [
{
"id": 1,
"name": "Stephenie Meyer",
"created_at": "2018-04-17T07:40:50.417Z",
"updated_at": "2018-04-17T07:40:50.417Z"
},
{
"id": 2,
"name": "V.C. Andrews",
"created_at": "2018-04-17T07:40:50.613Z",
"updated_at": "2018-04-17T07:40:50.613Z"
},
{
"id": 3,
"name": "Sophie Kinsella",
"created_at": "2018-04-17T07:40:50.646Z",
"updated_at": "2018-04-17T07:40:50.646Z"
}
]
arr.group_by { |e| e[:id] }
This will return
{
1 => [
{
:id => 1,
:name => "Stephenie Meyer",
:created_at => "2018-04-17T07:40:50.417Z",
:updated_at => "2018-04-17T07:40:50.417Z"
}
],
2 => [
{
:id => 2,
:name => "V.C. Andrews",
:created_at => "2018-04-17T07:40:50.613Z",
:updated_at => "2018-04-17T07:40:50.613Z"
}
],
3 => [
{
:id => 3,
:name => "Sophie Kinsella",
:created_at => "2018-04-17T07:40:50.646Z",
:updated_at => "2018-04-17T07:40:50.646Z"
}
]
}
I'm not sure the best way of approaching this; I've got a generic system for requesting data objects from an api and returning json by using ActiveModelSerializers and it's been fantasticly simple.
For one class I need to return data formatted differently than the normal class serilization and unsure of the best approach. No matter what it seems that I'm going to have to have an 'elsif' for this one class in the return (which pains me a little bit).
Currently I've got a class named 'curve' which a user has_many of, and so requesting a user's curve gets me something like this:
[{
"id": 7,
"name": "A",
"angle": 30,
"date": "2017-05-23T01:52:00.589-04:00",
"direction": "left",
"top": "C1",
"bottom": "C4",
"risser": 3,
"sanders": 8
}, {
"id": 8,
"name": "B",
"angle": 0,
"date": "2017-05-23T01:52:56.107-04:00",
"direction": "right",
"top": "C5",
"bottom": "C6",
"risser": null,
"sanders": null
}, {
"id": 9,
"name": "A",
"angle": 22,
"date": "2017-05-25T01:56:00.656-04:00",
"direction": "right",
"top": "C3",
"bottom": "C5",
"risser": null,
"sanders": null
}, {
"id": 11,
"name": "C",
"angle": 3,
"date": "2017-05-26T01:57:08.078-04:00",
"direction": "right",
"top": "C4",
"bottom": "C7",
"risser": null,
"sanders": null
}]
But I actually need each to be grouped by the name like so:
[{
"name": "A",
"points": [{
"id": 7,
"name": "A",
"angle": 30,
"date": "2017-05-23T01:52:00.589-04:00",
"direction": "left",
"top": "C1",
"bottom": "C4",
"risser": 3,
"sanders": 8
}, {
"id": 9,
"name": "A",
"angle": 22,
"date": "2017-05-25T01:56:00.656-04:00",
"direction": "right",
"top": "C3",
"bottom": "C5",
"risser": null,
"sanders": null
}]
},
{
"name": "B",
"points": [{
"id": 8,
"name": "B",
"angle": 0,
"date": "2017-05-23T01:52:56.107-04:00",
"direction": "right",
"top": "C5",
"bottom": "C6",
"risser": null,
"sanders": null
}]
},
{
"name": "C",
"points": [{
"id": 11,
"name": "C",
"angle": 3,
"date": "2017-05-26T01:57:08.078-04:00",
"direction": "right",
"top": "C4",
"bottom": "C7",
"risser": null,
"sanders": null
}]
}
]
Now I know I can do group_by and fiddle with the response - but even then it won't be using the default Curve serializer. I could also create a custom GroupedCurve serializer and possibly process each curve with a CurveSerializer - but at that point isn't it just like getting the default array and doing a map, and constructing my own hashes?
What's the best / cleanest way of processing data from top to the bottom format?
UPDATE:
Something like the following 'does the job' but the job is dirty:
def self.reformat_curves(curves)
arr = []
curves.each do |i|
alpha = i[:name]
entry = arr.find{|chunk| chunk[:name]==alpha}
if entry.nil?
arr << {name: alpha, points: [i]}
else
entry[:points] << i
end
end
return arr
end
I am trying to implement Like function in my rails app.
What I do for now is I have model Question, Answer and Like.
Basically,
Question model has question details for example user_id, question_text, Answer model has answer details for example user_id, answer_text and Like model has relationship for like between user_id, question_id, and answer_id.
In my Question controller, I want to render Question details page, where it will shows us a question and few answers.
I have render the question json, and answers related to the question.
{
"questions": [
{
"id": 1,
"text": "Turtoise",
"user_id": 22
}
],
"answers": [
{
"id": 41,
"question_id": 1,
"user_id": 17,
"text": "Good"
},
{
"id": 7,
"question_id": 1,
"user_id": 17,
"text": "alifff"
}
],
"isLike": [
{
"id": 1,
"user_id": "17",
"question_id": "1",
"answer_id": null,
"isLike": false
}
]
}
Now I want to render the json like this
{
"questions": [
{
"id": 1,
"text": "Turtoise",
"isLike": {
"id": 1,
"user_id": "17",
"question_id": "1",
"answer_id": null,
"isLike": true
}
}
],
"answers": [
{
"id": 7,
"text": "Good",
"isLike": {
"id": 2,
"user_id": "17",
"question_id": null,
"answer_id": "8",
"isLike": false
}
},
{
"id": 8,
"text": "Nice",
"isLike": {
"id": 3,
"user_id": "17",
"question_id": null,
"answer_id": "8",
"isLike": false
}
}
]
}
Can you give me some idea how to do this.
You may need to play with rails relationship here.
General speaking, your Question hasMany Like, and Like belongsTo Question. Same goes to Answer, Answer hasMany Like, and Like belongsTo Answer.
Since Like can belongsTo more than one model, you can try using Polymorphism Association.
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations