Dealing with single value hash or multiple value array in JSON data - ruby-on-rails

I have a JSON data feed where the value is a hash if single value and is an array if there are multiple values (see JSON data below).
JSON data:
# multiple values in option
{"options":{"option":[{"$t":"hasShots"},{"$t":"housebroken"}]}
# single value in option
{"options":{"option":{"$t":"hasShots"}}
I can do a check to see if the value is a hash or array using is_a? in Ruby, then extract the data accordingly and convert it to an object. Is this how it would typically be done or is there a better, more elegant way to code it in Ruby?
(NB: I figure this is such a common thing that there might be good solution to handling it. I google but it kept giving me how to parse JSON data's and creating JSON in Rails.)

I can do a check using is_a?. Is this how it would typically be done?
That's how I would do it! I think the more common scenario is for the JSON (or whatever) to have a consistent design, and have the objects in an array even if there's just one.

Related

Saving original order of keys in Dictionary by parsing JSONschema in Swift iOS

In my project I have to parse JSON schema, that comes from server.
It has object "Properties", which in fact like Dictionary in curly braces. And, of course, JSONSerialization.jsonObject parses it as Dictionary.
Everything looks like OK, BUT: I use these Properties for building my view (it defines fields to be fiiled by user). Finally, I have to save order of these fields! But, as we know, immediately after the object is parsed to Dictionary, it looses keys order. Anybody knows how can I parse these object, saving fields order?
Additional information:
Structure of Properties is build by user in WEB, so their count is avsolutely random for mobile client. Furthermore, Every object in properties (e.g. Group) can have its own properties, containing other objects. So we have absolutely random tree of nested objects. And their order is necessary for us.
If you don't care about interoperability, meaning 3rd parties also being able to rely on order, you can try to find a parser that preserves order (such as by reading it into an OrderedMap in Python instead of a regular dict- obviously this will differ by language.)
If you care about 3rd parties, it's trickier. As the last person to respond noted, JSON itself does not support this, and JSON Schema is just JSON as far as parsing goes.

Are dictionaries the presumed collection (array) in Xcode?

To use uisearchdisplaycontroller to specify a search on specific array fields, I found that the syntax caters only to key-value (dictionary?) data. Now, when I need to sort an array, I see again that the methods cater to key references, not mere array indexing. (I've built complex data with keyless arrays, not dictionaries.) Do I understand correctly that I should default to using dictionaries everywhere? Is there any situation to use plain arrays?
I see now the Key-Value Coding Guide (document 1812 of 3894)... should get to it any minute now.
It is NOT mandatory but it will be much comfortable when using NSDictionary.
Also, with the plain array, you can sort it as well without using "%K" at predicate format or using block comparator

What's the best way to allow users to input fields that will be serialized as hashes or arrays?

I have a field that is stored like this:
serialize :value
And, I'd like to store it as an array/hash/string depending on the input format. Is there an accepted best practice about how to handle this? The input will always come from the user.
If I understand your problem correctly, you are talking about the opposite of serialization, which is parsing or tokenzing (converting a string into meaningful pieces)... That's a very generic problem, but take a look at the String.match and String.scan to get you started. Here are some stack overflow quetions that could be helpful (remember to search for things like this first!)
What's the "ruby way" to parse a string for a single key/value?
How do I tokenize this string in Ruby?
The best practice is to serialize Arrays and Hashes. When it comes to string there is no need for serialization. HTML form elements like checkboxes posts the array, which you can be serialized and saved directly in table. For Hashes, you should manipulate yourself accordingly and apply to the attribute.

Concatenating JSON strings for POST request

I'm attempting to submit a large database containing many tables to a web service by sending the data via JSON. Extracting the data and converting it to a JSON string is working fine but so far I have only implemented it to send one table at a time each with its own ASIHTTPRequest. My question is whether or not concatenating all the JSON strings generated from each table is a good idea or if I should first combine the tables in their abstract data form, before converting all of them together to JSON?
Alternatively if there is any other suggestion that would be good too.
It entirely depends on your needs. If the tables are unrelated, multiple requests may be more appropriate because if a request fails (timeouts or loss of connection), it won't affect any other requests. However if you have tables with associations with one another, it would be better to send it all in one go so either all the data transmitted wholly or did not so you don't end up with broken associations.
You can't just "concatenate" JSON strings. The result will not be legal JSON. You need to somehow "splice" them.
And, of course, the server on the other end must be capable of parsing the resulting JSON -- it may only expect one table at a time, eg.
I dont see any issue in doing any one of the two choices you proposed
But i would suggest concatenate the tables in the database before converting so that you dont deal with string concatenations and other form of processes

Rails: Reconstructing ActiveRecord Objects from a JSON array

I have a JSON array with ActiveRecord objects. These objects can be reconstructed using the from_json method, which every AR object has. However with from_json it's only possible to reconstruct one single object.
To process an array, I could of course just extract substrings from the JSON array and create every object from it's own substring in a loop or so. However I'm wondering if there is a better way to do this, without string manipulation involved.
I would do
sudo gem install json
After that just
require "json"
and do
JSON.load(array_of_ar_json_representation)
or
JSON.parse(array_of_ar_json_representation)
what fits better for you.
Both of these methods return Ruby data structure that corresponds to the json structure. So, if you have a json array of obejcts, after JSON.load or JSON.parse you'll get Ruby array of hashes. You should't have any problems to manipulate this kind of structure.

Resources