How store data to array from json - ruby-on-rails

I have a json file in the format as follows:
{
"recipe_type": [
"vegetarian",
"non-vegetarian"
],
"recipe_times": [
"<30min",
"30-60min",
"60-90min"
],
"recipe_cuisines": [
"arabian",
"european",
"Indian",
"chinese"
]
}
I need to store each list to separate array:
#recipe = new JS.M.Recipe()
#recipe.fetch(#recipe.getRecipeType())
#recipeTypes = #recipe.getRecipeType()//Here data is getting as objetcs..if i use alert to print this. It showing output like "[object Object]"..
I want to store data as follows:
#recipe_type = #recipeTypes.get("recipe_type")
#recipe_times = #recipeTypes.get("recipe_times")//This is wrong
How can I achieve what I want?

If you are using backbone then you must be having underscorejs loaded on the page. If yes, this should work for you
lists = _.values(data)
where data is the JSON object mentioned above.

Related

Foreach controller not working for Json data

I have below json
[
{
"id": 13059,
"xid": "81f4278a53fe4c1a8d03346a34d76a47",
"yid": "8cbefbadec52ced5:e4f6801:17c73aeaf21:-5a75"
},
{
"id": 13059,
"xid": "061ef5792e8f4603bb1f86c71e6fb16c",
"yid": "8cbefbadec52ced5:e4f6801:17c73aeaf21:-5a56"
},
{
"id": 13061,
"xid": "4290987b25b34ffbb5b0329f1ab1b673",
"yid": "8cbefbadec52ced5:e4f6801:17c73aeaf21:-5dd4"
},
{
"id": 13063,
"xid": "57c4a2790aa44376be1e5215c5cb7ad0",
"yid": "8cbefbadec52ced5:e4f6801:17c73aeaf21:-585e"
}
]
Now I put the above json in global variable in JSR223 sampler
vars.put("jsonVariable",JSON.stringify(aboveJson))
Now next I add "ForEach Controller" and add
InputVariable Prefix = jsonVariable
Output variable name = outVariable
Then I add another JSR223 sampler inside ForEach Controller and log below data
log.info(${outVariable})
But ForEach controller is not even executing for once. Please let me know where I did wrong
ForEach Controller isn't smart enough to parse your JSON structure, it iterates an individual set of JMeter Variables which looks like:
var_1=foo
var_2=bar
etc.
so depending on what you're trying to achieve you need to:
In case if you want individual values of id, xid and/or yid - add 1 or more JSON Extractors and configure them to fetch the values from the response. Then use ForEach Controller and feed the variable from JSON Extractor to it
If you want to iterate JSON Objects instead you can use JSR223 PostProcessor and the following Groovy code:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).eachWithIndex { entry, int index ->
vars.put('json_' + (index + 1), new groovy.json.JsonBuilder(entry).toString())
}

Retrieving "$ref" field in swagger.json

I am trying to use the swagger parser to parse and retrieve information in the "swagger.json" (io.swagger.parser.SwaggerParser;)
Below is an excerpt of the "swagger.json".
I am trying to retrieve "$ref" : "#/definitions/abc".
"responses" : {
"200" : {
"description" : "abc",
"schema" : {
"$ref" : "#/definitions/abc"
}
},
This is the code to parse it.
SwaggerParser sparse = new SwaggerParser();
Swagger swagger = sparse.read("swagger.json");
// This next line is what I am having a problem with.
swagger.getPath("/endpointurl").getGet().getResponses().get("200").getSchema();
At this point, the ".getSchema()" in the above line has only "getType()" in it I can call. It doesn't have "get$ref()". This is because ".getSchema()" returns a "Property" (io.swagger.models.properties.Property). It doesn't have "get$ref()".
get$ref() is available in "RefProperty" (io.swagger.models.properties.RefProperty)
But ".getSchema()" doesn't return a "RefProperty".
Typecast the result of ".getSchema()" to a "RefProperty" also doesn't work. It ends up in this error.
java.lang.ClassCastException: io.swagger.models.properties.ArrayProperty cannot be cast to io.swagger.models.properties.RefProperty
Has anyone tried parsing a "swagger.json" and was able to retrieve the "$ref": line under "schema" in the "response" block?
Any idea how might I be able to do that?
I figured out a way to do that. Maybe not the best way to do it, but it retrieves the information in "#ref".
Object obj = xxxxx.getSchema(); // xxxxx is whatever your code that gives you ".getSchema()". Mine is in a map and I don't want to distract people.
ArrayProperty arrProp = new ArrayProperty();
arrProp = (ArrayProperty)obj; // .getSchema() returns an ArrayProperty
RefProperty refProperty = (RefProperty) arrProp.getItems(); // .getItems() will return the RefProperty type you need to call ".get$ref()".
String refStr = refProperty.get$ref(); // Voila, there's your content in "#ref".
String simpleRefStr = refProperty.getSimpleRef();
I had to do a few type castings. If you have a more elegant way of doing this, please post here.

Mongodb - search without key

I'd like to search documents in collection only by value. Let's say my collection contains documents like below:
[
{
"_id": "57a443c74d854d192afcc451",
"somekey": "123",
"otherkey": "zxc"
},
{
"_id": "57a443ca4d854d192afcc452",
"key": "123",
"otherkey": "123zxcvbnm"
}
]
and now I want to get all documents where value of any key contains 123.
I tried to do something like (written in Ruby and using mongoid):
new_search_query = { /.*/ => /#{v}/ }
collection.find(new_search_query)
but it looks like it is not suported becuase I get:
BSON::InvalidKey (Regexp instances are not allowed as keys in a BSON document.):
Is there any other manner or some workaround to do it?
Try full_text_search of mongoid for rails app.

How to query FQL Stream by Attachment.Media.Type?

When I query the stream table, how do I use attachment.media.type in a WHERE clause? The type field that is the sibling to attachment is always null and I've read that it was deprecated so that is why I want to access the one inside of attachment.
The structure of the attachment field array is:
"attachment": {
"name": "My Title",
"media": [
{
"href": "http://www.stackoverflow.com",
"alt": "",
"type": "link",
"src": "http://somephotourlhere"
}
]
I'd want to do something like this (which does not work):
WHERE attachment.media["type"] = "link"
The attachment.media field is an array. To the best of my knowledge, FQL can't return values from inside an array.
If you want to find links you can use AND type = 80 in your FQL.
However, Posted photos also have a type = 80. To filter these out, add
AND type = 80 AND attachment.fb_object_type != 'photo'
or
AND type = 80 AND strlen(attachment.fb_object_type) < 1
to protect against future objects that may also return type = 80. For links, attachment.fb_object_type is undefined.

Accessing specific values in array of JSON-Objects

I am using rails 3 with backbone.js 0.53 and currently receive a GET with the following array:
[{"credit_card":
{"id":2,"cc_number":"12345678912345","cc_type":"stack","owner":"overflow"}},
....next objects....]
I have read a lot of the other threads but can't figure out how to access the values.
Is there any way to do this with the backbone-given methods like .get()?
I tried
myArray = eval(arrayJSON)
alert myArray.length #works
but any other way of accessing the single values in an array or iterating over it fails.
Probably I am just missing something here.
Quick example of how to iterate through all your results via underscore:
/* received results mocking */
model.attributes = [
{ "credit_card" : { "id":2, "cc_number":"12345678912345" },
{ "credit_card" : { "id":3, "cc_number":"44444444455555" },
{ "credit_card" : { "id":4, "cc_number":"66666655554332" }
]
/* lets get all results */
results = model.toJSON()
/* loop through all results */
_(results).each(item) {
console.log(item.credit_card.id);
}
/* get result by array pos */
console.log(results[1]); // get 2nd item
Cheers
It might be useful to see more details, but in general you should be able to access elements just fine with the following syntax: alert(myArray[0].credit_card.cc_number);
I even pasted your sample array into jsFiddle and had no issues: http://jsfiddle.net/P4w7T/1/

Resources