How to handle object list request - ruby-on-rails

I am trying to read a list of objects
[
{
"name" : "Jhone",
"age" : 25
},
{
"name" : "Chris",
"age" : 24
}
]
How to handle that types of request?

You can loop through the array to access each object by its key. Assuming that array of objects is set to a variable called array
array.each { |element| puts element["name"] }
Btw, the objects are incorrectly formatted. You need a comma after each name value.

Related

Neo4j Rest API datetime data types

I am using neo4j REST API to create nodes and relationships. I refer following documentation.
https://neo4j.com/docs/http-api/3.5/actions/
The nodes that I am dealing with have dynamic properties. Therefore I am using following payload with parameters to create nodes.
{
"statements" : [ {
"statement" : "CREATE (n:Person $props) RETURN n",
"parameters" : {
"props" : {
"name" : "My Node",
"dob" : "datetime('20211229T153000')",
"age": 55,
"awsome": true
}
}
} ]
}
This is perfectly work with String, Integer, Boolean data types. However I need to use datetime data type for "dob" . If I use datetime function within double quotes (as in above example) neo4j treat it as string value and does not store as datatime data type.
Is there a solution for this?
You cannot put non-literal values inside the json data (props). However, you can change your cypher statement (create) to update the node and set the dob to a datetime field. But first you need to convert that string into datetime.
{
"statements" : [ {
"statement" : "CREATE (n:Person $props) SET n.dob = datetime({epochSeconds:apoc.date.parse(replace('20211229T153000','T', ' '),'s', 'yyyyMMDD HHmmss')}) RETURN n",
"parameters" : {
"props" : {
"name" : "My Node",
"age": 55,
"awsome": true
}
}
} ]
}
I replace the char 'T' into space so that it can be parsed. Then convert it into epoch seconds so that datetime will convert it properly. I tested it in my local neo4j so it should also work for you.
Result:

Serializing an array before sending it to an API end point

I am sending an array to an API endpoint, but the array is not going throuhg correctly. Here is what I have:
User:
name
email
So in my case I have a swift array of two users:
po userArray
▿ 2 elements
▿ [0] : <User: 0x7fa3ab597fd0>
▿ [1] : <User: 0x7fa3ab597bb0>
The JSON that the server expects is:
{users: [ {name:'john', email:'john#example.com'}, {name:'tom', email: 'tom#example.com' }]
What I'm sending:
'users' : userArray
But there seems to be a serialization issue. What the server is getting from me is this:
'users': ['MyApp.User', 'MyApp.User']
I'm using AlamoFire to send my PUT request.
Do I need to do convert the array to JSON before sending it out? I'm new to Swift and any help is appreciated.
You need to convert your User object into dictionary. Than you should add as value not array of users, but array of dictionaries.
You need to override description property for the custom class User
class User : NSObject
{
var name : String
var age : String
override var description : String
{
get{
return ["name" : self.name, "age" : self.age].description
}
}
}

How to permit array of array in rails

I have one request where one of the fields is array of array.
parameters
{
"product" => {
"pitch_points"=>{
"adwords"=>{
"search"=>{
"short"=>["qwe"],
"long"=>[["asdadas", "dasdasdas"]]
}
}
}
}
}
How can we permit this long key?
I have done like this
params.require(:product).permit(
pitch_points: [
adwords: [
search: [
short: [],
long: []
]
]
]
)
.my output is
{
"pitch_points"=>{
"adwords"=>{
"search"=>{
"short"=>["qwe"],
"long"=>[]
}
}
}
}
but output is empty long.
You can't explicitly permit it.
If you check the documentation it notes that strong parameters can be either a permitted scalar type or an array of permitted scalar types. Since an array is not a permitted scalar type you cannot whitelist an array nested within another array.

Swift Sort Dictionary by property of an object value

I have a dictionary like <String,Loto> and Loto is object like below;
Loto:
{
"success": true,
"data": {
"oid": "64kbbqi8dbxygb00",
"hafta": 961,
"buyukIkramiyeKazananIl": "",
"cekilisTarihi": "11/04/2015",
"cekilisTuru": "SAYISAL_LOTO",
"rakamlar": "03#02#48#16#15#08",
"rakamlarNumaraSirasi": "02 - 03 - 08 - 15 - 16 - 48",
"devretti": false,
"devirSayisi": 0,
"bilenKisiler": [
{
"oid": "64kbbxi8dbxyg403",
"kisiBasinaDusenIkramiye": 7.35,
"kisiSayisi": 185712,
"tur": "$3_BILEN"
},
{
"oid": "64kbbxi8dbxyg402",
"kisiBasinaDusenIkramiye": 53.05,
"kisiSayisi": 9146,
"tur": "$4_BILEN"
},
{
"oid": "64kbbxi8dbxyg401",
"kisiBasinaDusenIkramiye": 4532.2,
"kisiSayisi": 142,
"tur": "$5_BILEN"
},
{
"oid": "64kbbxi8dbxyg400",
"kisiBasinaDusenIkramiye": 1528438.75,
"kisiSayisi": 1,
"tur": "$6_BILEN"
}
],
"buyukIkrKazananIlIlceler": [
{
"il": "10",
"ilView": "BALIKESÄ°R",
"ilce": "01001",
"ilceView": "AYVALIK"
}
],
"kibrisHasilati": 51127,
"devirTutari": 0.09,
"kolonSayisi": 10537872,
"kdv": 1599672.97,
"toplamHasilat": 10537872,
"hasilat": 8938199.03,
"sov": 893819.9,
"ikramiyeEH": 8044379.129999999,
"buyukIkramiye": 1528432.03,
"haftayaDevredenTutar": 0
}
}
So my dictionary like <"11042015",Loto> and i want to sort this dictionary by "hafta" property of loto object.
How can i do this? Please help me!
If Loto is an object with a hafta property, you can sort your dictionary by passing it into the sorted function, along with a closure that tells it how to order the entries:
sorted(dict) { $0.1.hafta < $1.1.hafta }
($0.1 and $1.1 because dictionaries present as a sequence of key/value pairs - you want to sort by a property of the value i.e. tuple entry 1)
Note, this will give you back a sorted array, of type [(String,Loto)] pairs, rather than a dictionary (as Swift dictionaries are unordered).
(if Loto is not really an object but rather another dictionary, you might need to do {$0.1["hafta"] < $1.1["hafta"]} - it really depends on how you’re holding your data - the good news is you don’t need to worry about optionals, since they can be compared with <)
If you don’t need the keys, just sort the values:
sorted(dict.values) { $0.hafta < $1.hafta }
which will give you back a sorted array of type [Loto]
Dictionaries are not ordered. What you need is to convert it to array, and then sort it. So something like this.
let lotoArray = lotoDictionary.allObjects as [Loto]
lotoArray.sort { $0.hafta < $1.hafta }
You can't. The keys of a dictionary are not sorted. You can get an array of the values and sort that array.

RestKit JSON Mapping

I have given JSON and cannot parse partial data. It seems dictionary into dictionary:
{
"products": [
{
"id": 6796,
"title": "my title",
"description": "desc",
"code": "12345",
"valueType": "null",
"discounts": [
{
"minPrice": null,
"maxPrice": null,
"value": 20,
"avail": false
}
]
}
]
}
I am using the latest version of RESTKit but I cannot properly parse under discounts.
my RestKit settings are:
responseMapping.addAttributeMappingsFromDictionary([
"id" : "id",
"code" : "code",
"title" : "title",
"valueType" : "valueType",
"description" : "desc",
"discounts.minPrice" : "minPrice",
"discounts.maxPrice" : "maxPrice",
"discounts.value" : "value",
"discounts.avail" : "avail",
])
but all values below discounts always return Nil. What I am doing wrong?
You can't directly map using discounts.XXX because discounts is an array and you have no way to index into that array and extract a single value.
You either need to change the source JSON to compress the values out of the dictionary, or create a custom object that you can map each item in the discounts array to.
Technically you could map the whole discounts array, which would give you an array of dictionaries, that you could then unpack in the setter method, but the array of custom objects is usually a better approach.

Resources