SWIFT - String based Key-Value Array decoding? - macos

I have a String based Key-Value Array inside of a String, and I want to decoded it and assign the value to an existing array in Swift 4.2. For example:
let array: [String:String] = []
let stringToDecode = “[\“Hello\”:\”World\”, \"Key\":\"Value\"]”
// I want ‘array’ to be assigned
// to the value that is inside
// ‘stringToDecode’
I’ve tried the JSON decoder, but it couldn’t decode it. Is there a simple way to do this? Thank you.

Try using a library like SwiftyJson, it makes working with json much easier.

Related

Array concatenation in IBM Filenet P8 Expression Builder?

In the Expression Builder for the Workplace Process Designer, I have an attachment variable of type String[] (array of strings). I'd like to add some elements to it using the Expression Builder, but I can't work out the syntax. Has anyone done this? Is it even possible to add elements to an existing array in Expression Builder?
The arraytostring solution only works if there is only one element the array.
Assign the additional value with the following expression:
stringarray[elementcount(stringarray) + 1] = value
The expected 'array out of bound exception' and the resizing of the array is handled during the assignment.
I originally thought that it is not possible that we had to resort to have a custom java component to do the job, however I had run a small experiment that should serve as a workaround for your case.
Suppose you have String[] arrayType={"string1, string2"}, you can use the following expression as a value for your updated array:
{(arraytostring(arrayType, " ", " ,", ","))+"string3"}
What I did was that,
First, I used arraytostring function to convert the array to a string separated by comma with a comma left at the end. My output is similar to string1,string2,
Second, I appended the string that I want to add to the end of the string, so that my output is string1,string2,string3
Lastly, I assigned the value above to my array, using the array expression format {}, so my final evaluated string is {string1,string2,string3}
For more information about array functions, please follow the link below:
https://www.ibm.com/support/knowledgecenter/SSNW2F_5.2.1/com.ibm.p8.pe.user.doc/bpfe003.htm
I had a case like this, here is what I did:
I get the array from the attachment using the CE-Operation and store
in an array property on the workflow
Then I used the following to
{(arraytostring(workflowArray, " ", " ,", ","))+workflowStringProp}
using the CE-Operation again to set the array in the attachment with
the workflowArray.

Can't convert string to integer ruby error when accessing Hash/Array

I am new to ruby. So I was trying to get data from my data array but I get a "can't convert String into Integer" error.
The way I am accessing data is
data["myobject"]
It seem that data is an array, not a hash.
I think data looks like this :
data=['foo', 'bar']
instead of looking like this :
data={'myObject'=>'foo', 'myObject2'=>'bar'}
So try to change data or retrieve data by its index
data[0]

How do I pass an array of strings to a ruby method that wants the strings but not in an array?

I have an array of strings (actually file names), something like
filenames = ['file1.jpg', 'file2.jpg', 'file3.jpg']
The method I am calling expects something like
images = Magick::ImageList.new("image1.png", "image2.png", "image3.png")
but if I call as below, I am actually passing an array.
images = Magick::ImageList.new(filenames)
How do I unwrap the contents of the array?
Do as below using splat opearator(*) :
images = Magick::ImageList.new(*filenames)
As #Stefan mentioned the documentation link for the same Array to Arguments Conversion

Trying to extract key value from parsed JSON in ruby

I’m trying to parse some JSON from the twitter API and extract the value of a key (“media_url”), which is a sub-key of the key (“entities”)
so far I have:
url = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&screen_name=print_broadcast&count=1'
response = RestClient.get(url)
data=response.body
result = JSON.parse(data)
How would I extract a key value from the parsed JSON?
I’ve tried
result[“entities”]
etc, but I get en error when trying to convert a string to integer... the result of my parsed JSON is an array - shouldn't this be a hash?
Sorry for the dumb questions.
Any help would be appreciated.
The JSON output is actually a list. Granted, it only has one element, but it's still a list.
First get result[0], then you can access ['entries'].

How do I use a guid in a mongodb shell query

When using the MongoDB shell, how do I use a guid datatype (which I have used as the _id in my collection).
The following format doesn't work:
>db.person.find({"_id","E3E45566-AFE4-A564-7876-AEFF6745FF"});
Thanks.
You can use easily:
.find({ "_id" : CSUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")})
You have to compare the _id value against an instance of BinData (not against a string). Unfortunately the BinData constructor takes a Base64 string instead of a hex string.
Your GUID value is missing two hex digits at the end, so for the purposes of this example I will assume they are "00". The following values are equivalent:
hex: "E3E45566-AFE4-A564-7876-AEFF6745FF00" (ignoring dashes)
base64: "ZlXk4+SvZKV4dq7/Z0X/AA=="
So your query should be:
>db.person.find({_id : new BinData(3, "ZlXk4+SvZKV4dq7/Z0X/AA==")})
I am assuming that the binary subtype was correctly set to 3. If not, what driver was used to create the data?
You could use the following js function in front of your query like so:
function LUUID(uuid) {
var hex = uuid.replace(/[{}-]/g, ""); // removes extra characters
return new UUID(hex); //creates new UUID
}
db.person.find({"_id" : LUUID("E3E45566-AFE4-A564-7876-AEFF6745FF"});
You could save the function in .js file and load it or open it before you make your query and if you copy the value from your results you should rename the function with:
LUUID for Legacy UUID
JUUID for Java encoding
NUUID for .net encoding
CSUUID for c# encoding
PYUUID for python encoding
I know it's an old issue, but without any additional needs you can use this one:
find({_id:UUID('af64ab4f-1098-458a-a0a3-f0f6c93530b7')})
You can fix this issue by using split() and join() workaround:
for instance if I use "E3E45566-AFE4-A564-7876-AEFF6745FF" hex value with - inside UUID() function, it does not return BinData in mongo so please try removing all the - before passing to UUID function.
db.person.find({"_id":UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))});
Or by defining a variable to do it in multiple line:
var uuid = UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))
db.person.find({"_id":uuid});
or by creating a simple function:
function BUUID(uuid){
var str = uuid.split("-").join('');
return new UUID(str);
}
db.person.find({"_id": BUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")}).pretty();

Resources