Accessing Json with square brackets - ruby-on-rails

In Rails, how do I access the individual elements of this json? I parsed it using ruby's .to_json method.json = CSV.parse(csv).to_json.
Here's what is returned:
[
[
"id",
"subject"
],
[
"1",
"Economics"
],
[
"2",
"General Paper"
],
[
"3",
"History"
],
[
"4",
"Geography"
],
[
"5",
"Mathematics"
],
[
"6",
"Chemistry"
],
[
"7",
"Biology"
],
[
"8",
"Physics"
]
]
Trying to access json[0][1] or json[0] returns "["
I have a feeling it has to do with the square brackets but I'm sure there's a way to access it (It looks like arrays in an array to me and that means of access should work)

When you convert the data to JSON it becomes a JSON string. If you access it as an array you'll just get the character item referenced.
If you just want access to the data drop the to_json bit. The CSV parse should give you back the array of arrays and you can access it how you want.

Related

JSON key reducer and formatter in Ruby

I am facing a situation where it seems helpful to have a gem that can format the JSON that comes as a response from any API. For example I may make a call to fetch some data but I don't need all the fields, instead I need to create a new JSON by parsing the original one to only include some fields and also be able to select only some of nested fields from arrays or array of arrays inside that JSON. The result of the reduced JSON can be a JSON that has only one level of nesting.
Can you please help me if there is an existing gem in Ruby to do this?
I am showing an example to explain better my situation.
Original JSON:
[{
"key1": "test",
"key2": "Test",
"key3": "Test",
"key4": [
"key1": "test"
],
"key5": [
"key6": "test",
"key7": [
{"key8": ["test", "test"]},
{"key8": ["test2", "test3"]}
]
]
}]
And after executing some function, for example:
select(["key1", "key2", "key5.key6.key7.key8"])
The result would be:
[{
"key1": "test",
"key2": "Test",
"key8": [
"test", "test", "test2", "test3"
]
}]

Microsoft graph table api not returning column values in its date format

I'm retrieving data from Microsoft workbook/excel file's table.
Workbook's table has date-of-birth column which has date format.
While retrieving values using Microsoft graph's table api it returns dates as some integer value.
API i'm using:
https://graph.microsoft.com/v1.0/me/drive/root:/book.xlsx:/workbook/tables/table4/columns
Microsoft graph API reference : https://learn.microsoft.com/en-us/graph/api/table-list-columns?view=graph-rest-1.0&tabs=http
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('<user_id>')/drive/root/workbook/tables('table4')/columns",
"value": [
{
"#odata.id": "/users('<user_id>')/drive/root/workbook/tables(%27%7BF44C7573-9BBE-46F3-B0BE-C5607762D2AB%7D%27)/columns(%271%27)",
"values": [
[
"Name"
],
[
"Student 1"
],
[
"Student 2"
]
],
"id": "1",
"index": 0,
"name": "Name"
},
{
"#odata.id": "/users('<user_id>')/drive/root/workbook/tables(%27%7BF44C7573-9BBE-46F3-B0BE-C5607762D2AB%7D%27)/columns(%273%27)",
"values": [
[
"DOB"
],
[
35339
],
[
34249
]
],
"id": "3",
"index": 1,
"name": "DOB"
}
]
}
Original values are
Is there any way to get date values in its original format
The endpoint you are using
GET /me/drive/root:/book.xlsx:/workbook/tables/table4/columns
returns workbookTableColumn resource type which has only information about raw values without information about data type and formatting.
You can use id or name of workbookTableColumn and call dataBodyRange endpoint
GET /me/drive/root:/{item-path}:/workbook/tables/{id|name}/columns/{columnId|columnName}/dataBodyRange
GET /me/drive/root:/book.xlsx:/workbook/tables/{id|name}/columns/{columnId|columnName}/dataBodyRange
which returns range resource type.
Range resource type has property text which represents text values of the specified range.
...
"text": [
[
"09.06.2022"
],
[
""
]
],
...

fill a JSON file with embedded arrays from Rails

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.

How to sort multidimensional array in swift?

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.

How to represent typical json information

Below JSON contains 4 items in an array. If you look at each item it is some what shows incomplete keys. I am unable to figure out how to represent the consistent data in iOS (of any UI design patterns). By looking at the below information somewhat they are interlinked to each other for example parent key value information is same as company key value and also in employees one value is same as name key value. This seems to be very typical.
{
"arays": [
{
"company": "Microchip",
"parent": "File",
"employees": [
"John",
"Mike"
]
},
{
"company": "Apple",
"mobiles": [
"111-111-1111",
"121-121-1212"
]
},
{
"company": "File",
"parent": "Apple",
"addresses": [
"2600 space center blvd",
"2700 university dr"
]
},
{
"name": "John",
"mobiles": [
"222-222-2222"
],
"addresses": [
"Time Square, NY"
]
}
]
}

Resources