Parsing string to hash in ruby - arrays

I'm trying to parse a string in ruby to a hash but I can't quite figure it out. I've gotten it to a nested array just need to map it to the hash.
example string
subject = "{\"CN\"=\"schoen.io\", \"C\"=\"US\", \"ST\"=\"Texas\", \"L\"=\"North Doyle\", \"O\"=\"SSL Corporation\", \"OU\"=\"Information Technology Department\", \"2.5.4.17\"=\"16039-4645\", \"2.5.4.9\"=\"8268 Kemmer Village\", \"2.5.4.42\"=\"Tracy\", \"2.5.4.4\"=\"Jacobi\", \"2.5.4.5\"=\"grbh52f84senk4jkgo9n9a66yg62w78y4a0v36ax8tfacdshublxjpq6arcn7qyx\", \"2.5.29.17\"=\"ssl.com\"}}"
desired hash
{'CN' => 'schoen.io', 'C' => 'US', 'ST' => 'Texas',... }
my code
subject.gsub('{','').gsub('}','').split(',').map { |m| m.split('=')}
generated array
[["\"CN\"", "\"schoen.io\""], [" \"C\"", "\"US\""], [" \"ST\"", "\"Texas\""], [" \"L\"", "\"North Doyle\""], [" \"O\"", "\"SSL Corporation\""], [" \"OU\"", "\"Information Technology Department\""], [" \"2.5.4.17\"", "\"16039-4645\""], [" \"2.5.4.9\"", "\"8268 Kemmer Village\""], [" \"2.5.4.42\"", "\"Tracy\""], [" \"2.5.4.4\"", "\"Jacobi\""], [" \"2.5.4.5\"", "\"grbh52f84senk4jkgo9n9a66yg62w78y4a0v36ax8tfacdshublxjpq6arcn7qyx\""], [" \"2.5.29.17\"", "\"ssl.com\""]]

I think you have a typo in you original subject.
You have two } at the end of your string and only one at the beginning.
If you remove it your string is now :
subject = "{\"CN\"=\"schoen.io\", \"C\"=\"US\", \"ST\"=\"Texas\", \"L\"=\"North Doyle\", \"O\"=\"SSL Corporation\", \"OU\"=\"Information Technology Department\", \"2.5.4.17\"=\"16039-4645\", \"2.5.4.9\"=\"8268 Kemmer Village\", \"2.5.4.42\"=\"Tracy\", \"2.5.4.4\"=\"Jacobi\", \"2.5.4.5\"=\"grbh52f84senk4jkgo9n9a66yg62w78y4a0v36ax8tfacdshublxjpq6arcn7qyx\", \"2.5.29.17\"=\"ssl.com\"}"
You just need to do :
JSON.parse(subject.gsub('=',':'))
And you will get the desired output :
{
"CN"=>"schoen.io",
"C"=>"US",
"ST"=>"Texas",
"L"=>"North Doyle",
"O"=>"SSL Corporation",
"OU"=>"Information Technology Department",
"2.5.4.17"=>"16039-4645",
"2.5.4.9"=>"8268 Kemmer Village",
"2.5.4.42"=>"Tracy",
"2.5.4.4"=>"Jacobi",
"2.5.4.5"=>"grbh52f84senk4jkgo9n9a66yg62w78y4a0v36ax8tfacdshublxjpq6arcn7qyx",
"2.5.29.17"=>"ssl.com"
}

Related

VBA Access parsing JSON nested array

I am using VBA Access to get data from Google Books for a library database. The code is based on that given in this stackoverflow question.
I am struggling for the right code to allow for a varying number of authors as the information is in a nested array. I would like all of the author names to appear in one TextBox.
I tried:
Form_AddAmendItems.AuthorTextBox.Value = Join(subitem("authors"), ",")
from the link above but that fails to find any result.
I think I need to use UBound and LBound to count the number of authors and then loop through and add each one. But I haven't been able to find an example of how to do that.
Currently as a workaround I can populate the AuthorTextBox with the names of up to 3 authors, which is enough for my needs. But if there are less than 3 authors the error handler message pops up because it hasn't been able to find the requested data.
I am using the VBA-JSON Converter from here.
This is the JSON I would like to parse (from here)
{
"kind": "books#volumes",
"totalItems": 1,
"items": [
{
"kind": "books#volume",
"id": "BT2CAz-EjvcC",
"etag": "6Z7JqyUtyJU",
"selfLink": "https://www.googleapis.com/books/v1/volumes/BT2CAz-EjvcC",
"volumeInfo": {
"title": "Collins Gem German Dictionary",
"subtitle": "German-English, English-German",
"authors": [
"Veronika Calderwood-Schnorr",
"Ute Nicol",
"Peter Terrell"
]
And this is my VBA code:
Private Sub FindBookDetailsButton_Click()
'Error handle for Null Strings
If IsNull(Me.ISBNTextBox) = True Then
MsgBox "Item ID not specified.", vbExclamation + vbOKOnly, "Error"
Exit Sub
End If
'Error message if there is no match
On Error GoTo ErrMsg
Dim http As Object, JSON As Object, i As Integer, subitem As Object
Dim ISBN As String
ISBN = CStr(Me.ISBNTextBox.Value)
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", "https://www.googleapis.com/books/v1/volumes?q=isbn:" & ISBN, False
http.send
Set JSON = ParseJSON(http.responseText)
For Each item In JSON("items")
Set subitem = item("volumeInfo")
Form_AddAmendItems.TitleTextBox.Value = subitem("title")
Form_AddAmendItems.AuthorTextBox.Value = subitem("authors")(1)
Form_AddAmendItems.PublisherTextBox.Value = subitem("publisher")
'For multiple authors
Set subitem = item("volumeInfo")
If subitem.Exists("authors") Then
For Each item2 In subitem("authors")
Form_AddAmendItems.AuthorTextBox.Value = subitem("authors")(1) & ", " & subitem("authors")(2)
Next
For Each item3 In subitem("authors")
Form_AddAmendItems.AuthorTextBox.Value = subitem("authors")(1) & ", " & subitem("authors")(2) & ", " & subitem("authors")(3)
Next
End If
Next
'To end with success
MsgBox ("Process complete"), vbInformation
Exit Sub
'To end with an error message
ErrMsg:
MsgBox ("No match obtained"), vbCritical
End Sub
An array or collection or dictionary can be looped without knowing that object's limits. So if subitem("authors") is one of those object types, your code could be something like (essentially the code shown in accepted answer for the SO link in your question):
Set subitem = item("volumeInfo")
Form_AddAmendItems.TitleTextBox.Value = subitem("title")
Form_AddAmendItems.PublisherTextBox.Value = subitem("publisher")
If subitem.Exists("authors") Then
For Each item2 In subitem("authors")
sA = sA & item2 & ","
Next
sA = Left(sA, Len(sA) - 1) 'remove trailing comma
If sA <> "" Then Form_AddAmendItems.AuthorTextBox.Value = sA
End If
I discovered that elements in the ISBN download aren't always the same. As an example, with ISBN 0-575-05577-4 the publisher is not provided even though publishedDate is. Might use an If Then condition for each element you want to explicitly address. As a side note, found it interesting that the co-author for that book was not included.

Formula that splits data into separate sections based on a divider (blank cell)

I am trying to find a formula which splits data into separate columns based on a blank column.
As this is imported through another sheet and uses a divider:
try:
=INDEX(SUBSTITUTE(TRANSPOSE(IFERROR(SPLIT(TRIM(TRANSPOSE(
SPLIT(QUERY(SUBSTITUTE(IF(INDIRECT("A2:A"&MAX((ROW(A2:A))*(A2:A<>"")))="",
"×", A2:A), " ", "¤"),,9^9), "×"))), " "))), "¤", " "))
update:
=ARRAYFORMULA(IFERROR(1/(1/SUBSTITUTE(TRANSPOSE(IFERROR(SPLIT(TRIM(TRANSPOSE(
SPLIT(QUERY(SUBSTITUTE(IF(INDIRECT("A1:"&MAX((ROW(A1:A))*(A1:A<>"")))="",
"×", A1:A), " ", "¤"),,9^9+0), "×"))), " "))), "¤", " "))))

H2 - Field must be in group by list

i get the following exception: Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Field "RECHNUNGEN0_.GRUPPEN_RECHNUNGSJAHR" must be in GROUP BY List
can someone explain whats wrong in the query?
#Query("Select new de.company.jpa.model.RechnungsjahrUndNummerVO(" +
"r.gruppenRechnungsjahr, " +
"r.gruppenRechnungsnummer) " +
"From RechnungEntity r " +
"Where (:vermittlungsnummern is null or r.vermittlungsnummer in :vermittlungsnummern) " +
"and (:statusklassen is null or r.statusklasse in :statusklassen) " +
"and (:gruppenOm is null or r.gruppenOm = :gruppenOm)" +
"group by r.gruppenRechnungsnummer " +
"order by r.gruppenRechnungsjahr")
List<RechnungsjahrUndNummerVO> findRechnungsjahrUndNummernByVermittlungsnummernStatusklasseGruppenOm(List<String> vermittlungsnummern,
List<String> statusklassen,
String gruppenOm);

How to generate jose4j EllipticCurveJsonWebKey from JSON web key

I am able to create JWE as per https://bitbucket.org/b_c/jose4j/wiki/JWT%20Examples.
Problem: Unable to create JWE (EC type as mentioned below) using json web key.
Requirement: Create & Verify below type of JWE
"typ": "JWT",
"alg": "ECDH-ES+A256KW",
"enc": "A128CBC-HS256",
I want to avoid EcJwkGenerator.generateJwk(EllipticCurves.P256);
EllipticCurveJsonWebKey senderJwk = EcJwkGenerator.generateJwk(EllipticCurves.P256);
Can I create EllipticCurveJsonWebKey from the json web key?
Here is the json web key:
{
"kty":"EC",
"d":"648B3L4cIM8oMDPshuo3jeV5nd8XjMp3bVDjMQgXqhE",
"use":"enc",
"crv":"P-256",
"x":"w_UdBacxbKLLMbdvFaHWRK-O-GdnaBkRPtPaCQWcV44",
"y":"tHYH0m2uHIFNotcTJxwDLyykUtVHHd8XSXlFwyxJXNQ"
}
The first thing in https://bitbucket.org/b_c/jose4j/wiki/JWS%20Examples#markdown-header-using-the-rfc-7797-jws-unencoded-payload-option has this example:
// The public/private key pair for this example as a JWK
PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk("{" +
" \"kty\": \"EC\"," +
" \"d\": \"Tk7qzHNnSBMioAU7NwZ9JugFWmWbUCyzeBRjVcTp_so\"," +
" \"use\": \"sig\"," +
" \"crv\": \"P-256\"," +
" \"kid\": \"example\"," +
" \"x\": \"qqeGjWmYZU5M5bBrRw1zqZcbPunoFVxsfaa9JdA0R5I\"," +
" \"y\": \"wnoj0YjheNP80XYh1SEvz1-wnKByEoHvb6KrDcjMuWc\"" +
"}");
I do hope that the JWK you've posted is just an example. It has the private key, the d parameter, which is supposed to be kept private.

Why does DOORS `isBaseline` seem to always return FALSE?

I created a new major baseline in a DOORS module and then ran a small DXL script which includes lines
Module mp = current
Baseline bp = getMostRecentBaseline(mp)
int majorVersion = major(bp)
int minorVersion = minor(bp)
print "major " majorVersion " minor " minorVersion "\n"
string suff = suffix(bp)
print "suffix " suff "\n"
bool bstat
bstat = isBaseline(mp)
print "bstat " bstat "\n"
ModuleVersion mv = moduleVersion(mp)
string basind = baselineIndex(mp)
print "baseline index " basind "\n"
bool otherbstat = baseline(mp)
print "otherstat " otherbstat "\n"
bool basv = isBaseline(mv)
print "version base " basv "\n"
All of these return FALSE, indicating the module is not currently baselined. I have not done any edits to any attributes since baselining. I have done things like creating new Views. If I run the IBM DXL macro to compare the latest baseline against the "Current" version, it reports there are zero differences.
So my question is - what do the various isBaseline functions look at that is causing them to return FALSE? Or, am I going about this the wrong way - all I really need is a Q&D bit of DXL code to check that my module hasn't been edited for content since the last baseline was established.
The primary issue is that when your code gets a ModuleVersion (line 11), it uses a form of the function that gets the current version of the module. Line 14 should invoke isBaseline, not baseline, making the last two lines redundant.
See p310 of the current version (9.6.1) of the DXL Reference Manual for full details of the moduleVersion function.
The minimally modified version of your code that gets the result I think you were expecting, follows:
Module mp = current
Baseline bp = getMostRecentBaseline(mp)
int majorVersion = major(bp)
int minorVersion = minor(bp)
print "major " majorVersion " minor " minorVersion "\n"
string suff = suffix(bp)
print "suffix " suff "\n"
bool bstat
bstat = isBaseline(mp)
print "bstat " bstat "\n"
ModuleVersion mv = moduleVersion(uniqueID(mp), bp)
string basind = baselineIndex(mp)
print "baseline index " basind "\n"
bool otherbstat = isBaseline(mv)
print "otherstat " otherbstat "\n"
bool basv = isBaseline(mv)
print "version base " basv "\n"
In the version below, I've renamed variables, reordered some lines, and removed some content that wasn't required, for clarity:
Module modCurrent = current
Baseline blLatest = getMostRecentBaseline(modCurrent)
int iMajorVersion = major(blLatest)
int iMinorVersion = minor(blLatest)
string sBLSuffix = suffix(blLatest)
print "last baseline: major " iMajorVersion " minor " iMinorVersion " suffix " sBLSuffix "\n"
bool bIsBaseline = isBaseline(modCurrent)
print "bIsBaseline = " bIsBaseline "\n"
ModuleVersion mv = moduleVersion(uniqueID(modCurrent), blLatest)
Module modBaselined = load(mv, false)
string basind = baselineIndex(modBaselined)
print "baseline index = " basind "\n"
bIsBaseline = isBaseline(modBaselined)
print "bIsBaseline = " bIsBaseline "\n"
close(modBaselined)
Looks like isBaseline returns TRUE only if the current module View is set to display a selected baseline, as opposed to the "current" working view. `isBaseline and its brethren do not look at module contents, and thus won't see any potential differences between a baseline version and the current working view.
I'm aware of various DXL tools which perform a 'compare' on the contents, so that can be dealt with separately. As noted at this question, there are enhanced versions of the default "compare" script, such as one posted at this DOORS forum

Resources