I'm trying to display a json after different SQL queries. Some of the SQL queries return one value but I got an array of results from one of the queries. You can see the example of the json that I want to show below.
{
"id": "4",
"potential": "23",
"conversion": "45",
"new": "34",
"repeat": "22",
"average": "14",
"traffic": [
{
"time": "9",
"new": "2",
"repeat": "1"
},
{
"time": "10",
"new": "6",
"repeat": "9"
}
]
}
I can display separately hash and array as json, however I can't combine them.
{
"id": "4",
"potential": "23",
"conversion": "45",
"new": "34",
"repeat": "22",
"average": "14"
}
AND
[
{
"time": 5,
"new": 0,
"repeat": 80
},
{
"time": 6,
"new": 1,
"repeat": 80
}
]
Any suggestions? Thank you
Traverse the response you get and add the data into a Hash. Here is a ruby-doc for it. If you need a bit idea of how to use hash see below code.
x = {"id" => "4", "potential" => "23","conversion"=> "45","new"=> "34","repeat"=> "22","average"=> "14"}
x["traffic"] =[{"time"=>5,"new"=>0,"repeat"=>80},{"time"=>6,"new"=>1,"repeat"=>80}]
You would have to use your logic to build the hash. I just wrote something for you. Also you can then return the hash by render :json => x.
Related
I am trying to post a new row of data to an Excel table. I am sending a POST request to the Table / Rows. A new row is being added but with empty values. This happens both when I POST from code and also if I POST from the Microsoft Graph Explorer.
The POST statement I am using is:
POST /v1.0/drives/{driveid}/items/{itemid}/workbook/tables/{tableid}/rows
content-type: Application/Json
authorization: Bearer {access-token}
{
"value": [{
"values": [
["44444 : 22/08/2017 12:14:46",
"44444",
"22/08/2017 12:14:46",
"New Name",
"Status",
"01/10/2017 12:14:46",
"563",
"Filename"
]
],
"index": null
}]
}
I get a successful response message but with empty field values and the table has a blank row added.
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives({driveid)/items(itemid)/workbook/tables({tableid})/rows/$entity",
"#odata.id": "/drives({driveid})/items({itemid})/workbook/tables({tableid})/rows(null)",
"index": 2,
"values": [
[
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
]
}
How do I get the POST to include the values into the new row?
After more experimenting I have found what I have done wrong. My table has more columns in it than I was posting to. You need to post to all columns. I needed to add an additional two blank fields in this case. When the correct number of values was included the post works.
The JSON needs to be formatted like this as well as needing to have a value for each column in the table.
{ "values": [ [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29" ] ] }
I have a simple "rss" (ApplicationRecord) table indexed by an id. I would like to have a structured JSON that group each user from a family in an array structure. And then each family in a global array. How can I do that ?
my current plain code to put my data in a json file is :
json.rss #rss do |rs|
json.id rs.id
json.name rs.name
json.family rs.family
json.lastdate rs.lastdate
json.last rs.last
json.s1w rs.s1w
json.s2w rs.s2w
end
But the target file that I want is this one :
{
"rss": [
{
"familyname": "Smith",
"children": [
{
"id": "1",
"name": "bob",
"lastdate": "2010-09-23",
"last": "0.88",
"s1w": "0.83",
"s2w": "0.88"
},
{
"id": 2,
"name": "Mary",
"lastdate": "2011-09-23",
"last": "0.89",
"s1w": "0.83",
"s2w": "0.87"
}
]
},
{
"familyname": "Wesson",
"children": [
{
"id": "1",
"name": "john",
"lastdate": "2001-09-23",
"last": "0.88",
"s1w": "0.83",
"s2w": "0.88"
},
{
"id": 2,
"name": "Bruce",
"lastdate": "2000-09-23",
"last": "0.89",
"s1w": "0.83",
"s2w": "0.87"
}
]
}
]
}
The grouping you are trying to achieve can be done in Ruby with:
#rss.group_by(&:family).values
This is assuming #rss is an array-like collection of objects that have a .family method. The result: is an array of arrays of objects grouped by family.
Now it will be up to use to use Jbuilder's array! method to build the desired JSON output.
I am still trying to create an app where the user could transform his name or a word with chemical elements (like breaking bad logo).
The user will enter a word in a text field and when he'll submit it will return him the word with the corresponding chemical symbols if they match, or it will display the "raw" letters if they don't match.
ex: If no symbol matches I am keepking the initial entry so it could be: hello => He ll O (bold char represent the existing chemical symbols)
I know this could be done in js, but the challenge is ROR (btw I don't know any js...)
In an earlier question I had just a hash like:
symbols =
{"cr" => "Cr",
"sb" => "Sb",
"ag" => "Ag",
"ar" => "Ar",
"as" => "As",
"at" => "At",
"n" => "N",
"ba" => "Ba",
"bk" => "Bk"}
and I was using name.downcase.gsub!(Regexp.union(symbols.keys), symbols)to transform the user entry. Actually I need more datas... that's why I chosen the json file.
Like on this picture i will need to use:
"number"
"small"
"molar"
(and the "name" will appear in a caption below)
I have organized a .json file with all the symbols I may need in the app and stored it in my config/periodic_table.json (pasted just a sample cause it's very long).
1°) If a user enter "hello" how do I loop to search for the "he" hash and print the "name","number", "small" and the "molar"
2°) I will use the json as a database (I will use heroku to deploy) so do I have anything to transform for using json and pg together?
[ "symbols"
{
"h": {
"name": "Hydrogen",
"number": 1,
"small": "H",
"molar": 1.00794
},
"he": {
"name": "Helium",
"number": 2,
"small": "He",
"molar": 4.002602
},
"b": {
"name": "Boron",
"number": 5,
"small": "B",
"molar": 10.811
},
"c": {
"name": "Carbon",
"number": 6,
"small": "C",
"molar": 12.0107
},
"n": {
"name": "Nitrogen",
"number": 7,
"small": "N",
"molar": 14.0067
}
}
]
I will need to loop first with the symbols that contain 3 chars, then 2 then 1... shall i change anythin in the json, like an harray for the hashes that contains 3 chars, another for 2 chars , and for 1char?
Correct Json Format
[{
"symbols":{
"h":{
"name": "Hydrogen",
"number": 1,
"small": "H",
"molar": 1.0079
},
"he":{
"name": "Helium",
"number": 2,
"small": "He",
"molar": 4.002602
},
"b": {
"name": "Boron",
"number": 5,
"small": "B",
"molar": 10.811
},
"c": {
"name": "Carbon",
"number": 6,
"small": "C",
"molar": 12.0107
},
"n": {
"name": "Nitrogen",
"number": 7,
"small": "N",
"molar": 14.0067
}
}
}]
I need your help. I did an array, and when I launch my application on different devices or simulator, this array sorted into different types, but I need the same order on each device. How to do that?
var settings = [String: [ [String: String] ]]()
settings = [ "cards":[ ], "groups":[ ] ]
...
for card in db.prepare(table.order(orderN.asc)) {
settings["cards"]?.append(["id":String(card[id]), "group_id":String(card[group_id]), "service":card[service], "bgcolor":card[bgcolor], "logoimg":card[logoimg]!, "balance_desc":card[balance_desc], "balance":"0.0", "balance_id":String(card[balance_id]), "uniqueID":card[uniqueID], "balance_currency":String(card[balance_currency]), "orderN":String(card[orderN])])
}
...
for group in db.prepare(table.order(orderN.asc)) {
settings["groups"]?.append(["name":group[name]!,"id":String(group[id]),"orderN":String(group[orderN])])
}
...
For example,
On the first device
print(settings) // ["cards": [["orderN": "0", "bgcolor": "0.0, 0.0, 0.0, 1.0", "balance": "0.0", "logoimg": "example.com/images/img.png", "uniqueID": "00a2413f74f4a3f186e439a67057de67", "group_id": "2", "id": "1", "service": "servicename", "balance_desc": "Description", "balance_id": "1", "balance_currency": "1"]], "groups": [["orderN": "0", "name": "GroupName", "id": "2"]]]
On the second device
print(settings) // ["cards": [["orderN": "0", "uniqueID": "00a2413f74f4a3f186e439a67057de67", "service": "servicename", "id": "1", "bgcolor": "0.0, 0.0, 0.0, 1.0", "balance_currency": "1", "balance": "0.0", "group_id": "2", "logoimg": "example.com/images/img.png", "balance_id": "1", "balance_desc": "Description"]], "groups": [["id": "2", "orderN": "0", "name": "GroupName"]]]
Thank you for you attention.
That's because settings is not an Array, it's Dictionary. Order of key-value pairs in dictionary is not defined.
If you need some particular order, you should reimplement settings, probably make them separate struct or class, because you'll have a hard time working with nested dictionaries.
I am working on ASP.net MVC Applciation
I am facing difficulty in preparing json response to return from a controller action method.
I need some thing like this:
{
"cityMap": {"11": "Chennai", "12": "Mumbai", "13": "Delhi"},
"rows": [
{ "SID": "1", "SNAME": "ABC", "CITY": "11" },
{ "SID": "2", "SNAME": "XYZ", "CITY": "12" },
{ "SID": "3", "SNAME": "ACX", "CITY": "13" },
{ "SID": "4", "SNAME": "KHG", "CITY": "13" },
{ "SID": "5", "SNAME": "ADF", "CITY": "12" },
{ "SID": "6", "SNAME": "KKR", "CITY": "11" }
]
}
I have cityMap values in the sortedlist names slLocations
I have row values in the form of list liStudents
I am using the following syntax to send the json response:
return JSON(new { rows=liStudents},jsonRequestBehaviour.AllowGet)
By using the above syntax , i am getting the json response like this:
{
"rows": [
{ "SID": "1", "SNAME": "ABC", "CITY": "11" },
{ "SID": "2", "SNAME": "XYZ", "CITY": "12" },
{ "SID": "3", "SNAME": "ACX", "CITY": "13" },
{ "SID": "4", "SNAME": "KHG", "CITY": "13" },
{ "SID": "5", "SNAME": "ADF", "CITY": "12" },
{ "SID": "6", "SNAME": "KKR", "CITY": "11" }
]
}
Please help on how to extend json response to above by using liStudents and liLocations
Simply add the desired cityMap property to the anonymous object that you are passing to the view:
var model = new
{
cityMap = slLocations,
rows = liStudents,
}
return JSON(model, JsonRequestBehaviour.AllowGet);
This obviously assumes that the slLocations variable is a SortedList<string, string>.