convert json string to hash and retrieve hash items values in ruby - ruby-on-rails-3

I have a string object which is basically in a json format and while trying to print it shows in console as
item =
{
"id": "4c9f83e4-f479-48d0-9f92-3fff70a8f6ba",
"item": "{"business":"1114","class":"Demo","date":"01-01-2014","version":"","data":"dummy","name":"Finance"}"
}
I need to get the values of business, class, date etc and pass it as params to my method. So I tried to convert it into hashes as below
hash_item = JSON.parse (item)
and output in console shows as
The converted hash item is
{"guid"=>"4c9f83e4-f479-48d0-9f92-3fff70a8f6ba", "item"=>"{"business":"1114","class":"Demo","date":"01-01-2014","version":"","data":"Dummy","name":"Finance"}"}
But when I try to access the hash value for business as
hash_item['item']['business'] it shows
"business"
since the value of item is a String in the hash_item. I am not sure whether my approach is correct or not. So is there any better idea or any inputs to retrieve the hash values . Please help.
Updated Code:
Actually I am implementing a queuing mechanism with right_aws gem for my application and the queue item returns a Message Instance of String format. so in my controller I am accessing the queue object as
def next
#receiver = #queue.pop()
if #receiver.present?
respond_with(#receiver)
end
end
And my next.json.erb would be
{
"guid": "<%=#receiver.id%>",
"item": "<%=generate_json_format(#receiver)%>"
}
my helper to generate json format is
def generate_json_format(sender)
(JSON.parse (sender.body.gsub('=>', ':'))).to_json
end

Related

(Problem solved) Set the value of a livedata variable of type <data class> to a list of strings?

How to populate the value of this variable:
private val _urlList = MutableLiveData<List<Url>>()
of type Url:
data class Url(
val imgSrcUrl: String
)
with the incoming list of url strings from a firebase call?
Here is where the magic happens:
private fun getData(){
viewModelScope.launch {
try {
getImagesUrl {
"Here where I need to set the value of the variable to a listOf(it) with it being strings
of urls retrieved from firebase storage"
}
}catch (e: Exception){
"Handling the error"
}
}
}
Edit
The map function #dominicoder provided solved my problem, answer accepted.
Thank you all for your help
Your question is unclear because you're showing a live data of a single Url object but asking to stuff it with a list of strings. So first, your live data object needs to change to a list of Urls:
private val _urlList = MutableLiveData<List<Url>>()
Then, assuming getImagesUrl yields a list of strings, if I understood you correctly, then you would map that to a list of Urls:
getImagesUrl { listOfImageUrlStrings ->
_urlList.value = listOfImageUrlStrings.map { imageUrlString -> Url(imageUrlString) }
}
If that does not answer your question, you really need to review it and clarify.
You can set values on the MutableLiveDataObject in two ways (depends on what you're doing).
Setting the value as normal from the UI thread can be done with:
myLiveData.value = myobject
If you're setting it from a background thread like you might in a coroutine with a suspended function or async task etc then use:
myLiveData.postValue(myObject)
It's not clear from your question whether the LiveData is meant to hold a list as you mention both lists and single values. But your LiveData holds a set the values as a collection like a list, set or map. It's can be treated as a whole object so adding a value later needs to have the whole collection set again like:
myLiveData.value = mutableListOf<Url>()
//Response received and object created
myLiveData.value = myLiveData.value.apply {
add(myObject)
}
Or if the value is mutable updating the existing value (preferred as it's cleaner):
myLiveData.value.add(myObject)
The problem with that approach is you're exposing the map as a mutable/writeable object. Allowing accessors to change the values which you might not want.

How to set array property value in code using Carina Test Framework for API tests?

I have the following request json body:
{
...
"attachmentIds": "${attachments}"
...
}
I have a properties file that includes the declaration of the corresponding placeholder
I want to set array of strings in code instead of "attachments" placeholder, but getProperties().setProperty() expects only string value.
How can I achieve it other way or is it possible at all?
Thanks!
As an option you can transform your array into the String in java code. And then pass this String as property value.
Another option, you can pass String array from code and then parse it in your json template.
For example:
String[] arr = { "1", "2", "3" };
apiMethod.addProperty("attachments", arr);
And then in your json:
{
"attachmentIds": [<#list attachments as val>"${val}"<#if val?has_next>,</#if></#list>]
}
Check freemarker documentation to get more details:
https://freemarker.apache.org/docs/ref_builtins_loop_var.html
Also please note that some of freemarker functions (including has_next) are available only in newest versions of library. So make sure to add into your dependencies list. Carina is now in process of migrating to latest freemarker version.

Parse string into JSON

I have an array of objects $arr and an object has a property named as jsonData which contains json data in a string, how to parse that string to actual JSON object and retrieve lets say value for key name? I tried this:
#foreach ($obj in $arr)
#set ($jsonData = "#evaluate(${obj.jsonData})")
$jsonData.get("name") ## <-- not working
#end
If anyone using velocity in AWS API Gateway ends up here, you can use AWS'
$util.parseJson() to covert a string to JSON.
Make sure you note if your string is using single or double quotes. You may need to use $util.escapeJavaScipt.replaceAll() before parsing the string.
Fixed it like this:
#foreach ($obj in $arr)
#set( $jsonData = '#set( $jsonData = ' + $obj.jsonData + ' )' )
#evaluate ($jsonData)
$jsonData.get("name") ##<-- works now
#end
I was using velocity 1.7
This is mainly AWS lambda (in python) response template related.. so.. if you want to return custom response code with json it is easy to return JSON in node js but when it comes to python as per i know, we cant raise dict as a value. So.. this may help. If you are doing like..
raise Exception({"responseCode": 400, "response": "missing :\"recipientCount\""})
It will throw exception in errorMessage key and value as a string.
To overcome this..
Add response code 400 in Method Response
regExp in Integration Response :
.*'responseCode': 400.*
Add Body Mapping Template as application/json:
$util.escapeJavaScript( $input.path('$.errorMessage')).replaceAll("\\'",'\"')
Make sure you are not using single quotes in value in exception string.
Wrong:
raise Exception({"responseCode": 400, "response": "missing :'recipientCount'"})
Right:
raise Exception({"responseCode": 400, "response": "missing :\"recipientCount\""})
I know it is not best solution... but this is the only solution i have found. Feel free if you have better solution.

vTiger webservice update operation

im trying to do an update operation from angular 2 but i dont know how to pass the element parameter
in the vtigercrm.log i see DEBUG webservice -
array ('element' => NULL)
which leads to this
"error": {
"code": "ACCESS_DENIED",
"message": "Permission to perform the operation is denied for id",
i tryied passing the following object as a JSON
{
"subject":"test2",
"assigned_user_id":"19x1",
"date_start":"2016-11-15",
"time_start":"12:00:00",
"due_date":"2016-11-15",
"time_end":"14:00:00",
"recurringtype":"",
"duration_hours":"2",
"duration_minutes":"0",
"parent_id":"",
"eventstatus":"Planned",
"sendnotification":"0",
"activitytype":"Call",
"location":"",
"createdtime":"2016-11-15 11:31:19",
"modifiedtime":"2016-11-15 11:31:19",
"taskpriority":"",
"notime":"0",
"visibility":"Public",
"modifiedby":"19x1",
"description":"",
"reminder_time":"",
"contact_id":"",
"latitud":"",
"longitud":"",
"id":"18x22029"
}
the same thing as encodeURI and encodeURIComponent but nothing works
I had a similar error, and I found a subtle difference in the JSON. When a result was returned, the json object is the first element in an array. But for sending, it needs to be just the object, not an array.
<?php
//decode the json encode response from the server.
$jsonResponse = json_decode( $response->getBody(), true );
//Get first array element for sending back with update
$objectJson = json_encode($jsonResponse[ 'result' ][0]);
?>
Whenever I tried sending the original response, I received an error: {"success":false,"error":{"code":"ACCESS_DENIED","message":"Permission to perform the operation is denied for id"}}
This error message can be due to:
Object ID not mentionned/correct in the JSON objet that you send as the parameter "element"
Vtiger Session ID not sent/correct in the parameter "sessionName"
See here an example (in PHP) of how to pass the object to perform an update

Why does storing a Nancy.DynamicDictionary in RavenDB only save the property-names and not the property-values?

I am trying to save (RavenDB build 960) the names and values of form data items passed into a Nancy Module via its built in Request.Form.
If I save a straightforward instance of a dynamic object (with test properties and values) then everything works and both the property names and values are saved. However, if I use Nancy's Request.Form then only the dynamic property names are saved.
I understand that I will have to deal with further issues to do with restoring the correct types when retrieving the dynamic data (RavenJObjects etc) but for now, I want to solve the problem of saving the dynamic names / values in the first place.
Here is the entire test request and code:
Fiddler Request (PUT)
Nancy Module
Put["/report/{name}/add"] = parameters =>
{
reportService.AddTestDynamic(Db, parameters.name, Request.Form);
return HttpStatusCode.Created;
};
Service
public void AddTestDynamic(IDocumentSession db, string name, dynamic data)
{
var testDynamic = new TestDynamic
{
Name = name,
Data = data
};
db.Store(testDynamic);
db.SaveChanges();
}
TestDynamic Class
public class TestDynamic
{
public string Name;
public dynamic Data;
}
Dynamic contents of Request.Form at runtime
Resulting RavenDB Document
{
"Name": "test",
"Data": [
"username",
"age"
]
}
Note: The type of the Request.Form is Nancy.DynamicDictionary. I think this may be the problem since it inherits from IEnumerable<string> and not the expected IEnumerable<string, object>. I think that RavenDB is enumerating the DynamicDictionary and only getting back the dynamic member-names rather than the member name / value pairs.
Can anybody tell me how or whether I can treat the Request.Form as a dynamic object with respect to saving it to RavenDB? If possible I want to avoid any hand-crafted enumeration of DynamicDictionary to build a dynamic instance so that RavenDB can serialise correctly.
Thank You
Edit 1 #Ayende
The DynamicDictionary appears to implement the GetDynamicMemberNames() method:
Taking a look at the code on GitHub reveals the following implementation:
public override IEnumerable<string> GetDynamicMemberNames()
{
return dictionary.Keys;
}
Is this what you would expect to see here?
Edit 2 #TheCodeJunkie
Thanks for the code update. To test this I have:
Created a local clone of the NancyFx/Nancy master branch from
GitHub
Added the Nancy.csproj to my solution and referenced the project
Run the same test as above
RavenDB Document from new DynamicDictionary
{
"Name": "test",
"Data": {
"$type": "Nancy.DynamicDictionary, Nancy",
"username": {},
"age": {}
}
}
You can see that the resulting document is an improvement. The DynamicDictionary type information is now being correctly picked up by RavenDB and whilst the dynamic property-names are correctly serialized, unfortunately the dynamic property-values are not.
The image below shows the new look DynamicDictionary in action. It all looks fine to me, the new Dictionary interface is clearly visible. The only thing I noticed was that the dynamic 'Results view' (as opposed to the 'Dynamic view') in the debugger, shows just the property-names and not their values. The 'Dynamic view' shows both as before (see image above).
Contents of DynamicDictionary at run time
biofractal,
The problem is the DynamicDictionary, in JSON, types can be either objects or lists ,they can't be both.
And for dynamic object serialization, we rely on the implementation of GetDynamicMemberNames() to get the properties, and I assume that is isn't there.