How to convert in hive a string representation of array of json objects back to array form - sql

I am working in hive / SQL. I have a column in my table with strings which represent an array of json objects. I need to convert the strings to arrays of JSON strings.
For example, I have this,
"[{a:1, b:1},{a:2, b:2}]"
And I want to get this:
["{a:1, b:1}","{a:2, b:2}"]
Tried casting the string as array but that didn't work. Any ideas on how do this in a smart way short of splitting by "},{"?

never mind, I ended up just splitting the string on "}" and then adding back the "}" to each piece, worked well!

Related

Find records where length of array equal to - Rails 4

In my Room model, I have an attribute named available_days, which is being stored as an array.
For example:
Room.first.available_days
=> ["wed", "thurs", "fri"]
What is the best way to find all Rooms where the size of the array is equal to 3?
I've tried something like
Room.where('LENGTH(available_days) = ?', 3)
with no success.
Update: the data type for available_days is a string, but in order to store an array, I am serializing the attribute from my model:
app/models/room.rb
serialize :available_days
Can't think of a purely sql way of doing it for sqlite since available_days is a string.
But here's one way of doing it without loading all records at once.
rooms = []
Room.in_batches(of: 10).each_record do |r|
rooms << r if r.available_days.length == 3
end
p rooms
If you're using postgres you can parse the serialized string to an array type, then query on the length of the array. I expect other databases may have similar approaches. How to do this depends on how the text is being serialized, but by default for Rails 4 should be YAML, so I expect you data is encoded like this:
---
- first
- second
The following SQL will remove the leading ---\n- as well as the final newline, then split the remaining string on - into an array. It's not strictly necessary to cleanup the extra characters to find the length, but if you want to do other operations you may find it useful to have a cleaned up array (no leading characters or trailing newline). This will only work for simple YAML arrays and simple strings.
Room.where("ARRAY_LENGTH(STRING_TO_ARRAY(RTRIM(REPLACE(available_days,'---\n- ',''),'\n'), '\n- '), 1) = ?", 3)
As you can see, this approach is rather complex. If possible you may want to add a new structured column (array or jsonb) and migrate the serialized string into the a typed column to make this easier and more performant. Rails supports jsonb serialization for postgres.

Convert a StringBuilder to a Jagged Array

I have built a VB.Net class that will be used in VBA for reading text files. I've set it up so the user can specify what tables in the file he wants to return. What I have done is build a StringBuilder of the tables, then return it as a jagged array, but I can't quite get the conversion of the builder to array part right. I'd like the the first level to be split on "NewLine" and the second level to be split on ",".
Is this possible without having to use multiple arrays and\or loops?
This will create the jagged array:
Dim myArray = (From row In myStringBuilder.ToString().Split({vbCrLf}, StringSplitOptions.None)
Select (From col In row.Split(","c)
Select col
).ToArray()
).ToArray()
Explanation:
First, we convert the StringBuilder to a String: myStringBuilder.ToString()
Then we split on line breaks: Split({vbCrLf}, StringSplitOptions.None). Since a line break consists of two characters in Windows, we use the Split overload that accepts a string array (hence the braces).
Within the row we split the line on commas: Split(","c). The c specifies that this is a character instead of a string.
Finally, we convert this enumerable of enumerables into an array of arrays by applying ToArray to the outer as well as the inner LINQ expression.
You could represent your jagged array using nested lists and generics. The outer (row) would be a generic list and the inner (col) could be a list of strings.
Other approaches could leverage XML or LINQ but would be less efficient.

can vb.net read query strings with field[] format?

I'm trying to navigate through not being able to read multidimensional arrays with JavaScriptSerializer.
I think there's a workaround if I can do what's in this answer https://stackoverflow.com/a/9547490/1382306
Basically, if I can store json arrays in each field[] and loop through field, it should be no problem.
How do I loop through field if it's in the query string of this format
?field[]=["a","b","c"]&field[]=["d","e","f"]
Try
Request.QueryString ["field[]"][0]
... to return:
["a","b","c"] {in quotes}
and
Request.QueryString ["field[]"][1]
... to return:
["d","e","f"]
You will have to strip off the square brackets and then use split () over the commas.

JSON string containing regular expression as data

I am reading JSON code from the database and then parsing the string using json parsers available for java. But I am getting JSONexception. Even if I try to parse this string on an online parser http://json.parser.online.fr/ there also the strings are taken as errors. Is there a way out to get rid of these errors or in other words how can I take care of such special symbols. The value of match is a regular expression.
Here is subpart of the sample string I am trying to parse as a json object.
{"RULE":[{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src="[^"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\s*</a>","type":"text"}},{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src="[^"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\s*</a>","type":"text"}}]}
use this json
{"RULE":[{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src=\"[^\"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\\s*</a>","type":"text"}},{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src=\"[^\"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\\s*</a>","type":"text"}}]}

sorting and getting uniques

i have a string that looks like this
"apples,fish,oranges,bananas,fish"
i want to be able to sort this list and get only the uniques. how do i do it in vb.net? please provide code
A lot of your questions are quite basic, so rather than providing the code I'm going to provide the thought process and let you learn from implementing it.
Firstly, you have a string that contains multiple items separated by commas, so you're going to need to split the string at the commas to get a list. You can use String.Split for that.
You can then use some of the extension methods for IEnumerable<T> to filter and order the list. The ones to look at are Enumerable.Distinct and Enumerable.OrderBy. You can either write these as normal methods, or use Linq syntax.
If you need to get it back into a comma-separated string, then you'll need to re-join the strings using the String.Join method. Note that this needs an array so Enumerable.ToArray will be useful in conjunction.
You can do it using LINQ, like this:
Dim input = "apples,fish,oranges,bananas,fish"
Dim strings = input.Split(","c).Distinct().OrderBy(Function(s) s)
I'm not a VB.NET programmer, but I can give you a suggestion:
Split the string into an array
Create a second array
Cycle through the first array, adding any value that is not in the second.
Upon completion, your second array will have only unique values.