How to generate a JSON string from an Array in RUBY on RAILS - ruby-on-rails-5

I have an issue while generating json object from an Array using Ruby on Rails (i am just like beginner in this area) ,
I have an array like
questions= [{
"question_cd": "Question1002",
"value": {
"value": "NO"
}
},
{
"question_cd": "Question30",
"value": {
"value": "NO"
}
}, {
"question_cd": "Question138",
"value": {
"value": "NO"
}
}, {
"question_cd": "Question870",
"value": {
"value": "NO"
}
}, {
"question_cd": "Question1524",
"value": {
"value": "NO"
}
}, {
"question_cd": "Question1781",
"value": {
"value": "NO"
}
}, {
"question_cd": "Question2084",
"value": {
"value": "YES"
}
}
]
Generate Json string :
questions. each do |dt|
questioncode = dt&.dig('question_cd')
value = dt&.dig('value')
questioncode = dt&.dig('question_cd')
value = dt&.dig('value')
questiondt = '"'+ questioncode+'": '+'"'+value+'"'
#finalquestion = questiondt if #finalquestion.blank?
#finalquestion = #finalquestion+', '+questiondt if !#finalquestion.blank?
end
#questions_json = "{ #{#finalquestion} }"
using above array i need to generate a JSON object like
{
"Question1002": "NO",
"Question30": "NO",
"Question138": "NO",
"Question870": "NO",
"Question1524": "NO",
"Question1781": "NO",
"Question2084": "YES",
}
An this should be return as Json string (in RUBY ON RAILS) like
def json_questions
{
"questionslist" : #questions_json
}
end
I am able to get a json string but it added "" char while return to parent method . Here is the output what i get
{ "questionslist": "{ \"Question1002\": \"'NO\",\"Question30\": \"'NO\",\"Question138\": \"'NO\",\"Question870\": \"'NO\",\"Question1524\": \"'NO\",\"Question1781\": \"'NO\",\"Question2084\": \"'NO\"}",
"Description of Operations": "test description"
}
Please help me how to return JSON string with out ''
Thanks in Advance.

Related

How to check a particular value on basis of condition in karate

Goal: Match the check value is correct for 123S and 123O response in API
First check the value on this location x.details[0].user.school.name[0].codeable.text if it is 123S then check if x.details[0].data.check value is abc
Then check if the value on this location x.details[1].user.school.name[0].codeable.text is 123O then check if x.details[1].data.check is xyz
The response in array inter changes it is not mandatory first element is 123S sometime API returns 123O as first array response.
Sample JSON.
{
"type": "1",
"array": 2,
"details": [
{
"path": "path",
"user": {
"school": {
"name": [
{
"value": "this is school",
"codeable": {
"details": [
{
"hello": "yty",
"condition": "check1"
}
],
"text": "123S"
}
}
]
},
"sample": "test1",
"id": "22222"
},
"data": {
"check": "abc"
}
},
{
"path": "path",
"user": {
"school": {
"name": [
{
"value": "this is school",
"codeable": {
"details": [
{
"hello": "def",
"condition": "check2"
}
],
"text": "123O"
}
}
]
},
"sample": "test",
"id": "11111"
},
"data": {
"check": "xyz"
}
}
]
}
How I did in Postman but how to replicate same in Karate?
var jsonData = pm.response.json();
pm.test("Body matches string", function () {
for(var i=0;i<jsonData.details.length;i++){
if(jsonData.details[i].user.school.name[0].codeable.text == '123S')
{
pm.expect(jsonData.details[i].data.check).to.equal('abc');
}
if(jsonData.details[i].user.school.name[0].codeable.text == '123O')
{
pm.expect(jsonData.details[i].data.check).to.equal('xyz');
}
}
});
2 lines. And this takes care of any number of combinations of lookup values :)
* def lookup = { '123S': 'abc', '123O': 'xyz' }
* match each response.details contains { data: { check: '#(lookup[_$.user.school.name[0].codeable.text])' } }

how can convert string into json result

I am using function to get the data as a tree format. the data return from GetDepotWithDepartment as a string datatype. how can I convert the string into JsonResult
public JsonResult GetDepotDepartemntsForMap()
{
string lson = _unitOfWork.Department.GetDepotWithDepartment();
return lson // error is coming cannot convert string into mvc.jsonREsult
}
The data coming from the repository linq as given
[
{
"id": 1,
"title": "1-Depot1",
"subs": [
{
"id": "1.1",
"title": "1.Advt"
},
{
"id": "1.2",
"title": "1.Admin"
}
]
},
{
"id": 2,
"title": "2-Depot2",
"subs": [
{
"id": "2.1",
"title": "2.Sales"
},
{
"id": "2.2",
"title": "2.Admin"
}
]
}
]
According to your description, I suggest you could try below codes to return the json result:
public JsonResult GetDepotDepartemntsForMap()
{
string lson = _unitOfWork.Department.GetDepotWithDepartment();
return new JsonResult(lson) // error is coming cannot convert string into mvc.jsonREsult
}
Result:

POSTGRESQL query to extract attributes in JSON

I have the below JSON in a particular DB column. I need a query to extract fields stored within the savings rate(to and from).
{
"data": [
{
"data": {
"intro_visited": {
"portfolio_detail_investment_journey": true,
"dashboard_investments": true,
"portfolio_list_updates": true,
"portfolio_detail_invested": true,
"portfolio_list_offering": true,
"dashboard_more_bottom_bar": true
}
},
"type": "user_properties",
"schema_version": "1"
},
{
"data": {
"savings_info": {
"remind_at": 1583475493291,
"age": 100,
"savings_rate": {
"to": "20",
"from": "4"
},
"recommendation": {
"offering_name": "Emergency Fund",
"amount": "1,11,111",
"offering_status": "not_invested",
"ideal_amount": "1,11,111",
"offering_code": "liquid"
}
}
},
"type": "savings_info",
"schema_version": "1"
}
]
}
To get the "To"
$..data.savings_info.savings_rate.to
To get the "From"
$..data.savings_info.savings_rate.from
This script works
SELECT
<column> ->'data'->2->'data'->'savings_info'->'savings_rate'->>'to' AS to_rate
from <table>

Extract data from json array in Karate

In the below JSON response, I need to extract the 'cid' for the record that has the 'nationalityDecription' as 'USA'. By using this query as a reference, I used the below loc in the karate feature file, but 1st line itself fails with syntax error(tried different combinations). For now, I'm using the custom javascript as a workaround which is working fine. I need help to check if i'm missing anything in syntax. Thanks
Response:
{
"header": {
"Id": "12345678",
"timeStamp": "2018-09-17T10:09:812.000"
},
"dataRecords": [
{
"cid": "31H678",
"cidMeta": "00",
"nationalityDecription": "CHINA"
},
{
"cid": "31S421",
"cidMeta": "01",
"nationalityDecription": "USA"
}
]
}
Feature file:
* def record= $response.dataRecords[?(#.nationalityDecription=='USA')]
* def cid = record.cid
* def response = { "header": { "Id": "12345678", "timeStamp": "2018-09-17T10:09:812.000" }, "dataRecords": [ { "cid": "31H678", "cidMeta": "00", "nationalityDecription": "CHINA" }, { "cid": "31S421", "cidMeta": "01", "nationalityDecription": "USA" } ] }
* def cid = get[0] response.dataRecords[?(#.nationalityDecription=='USA')].cid
* match cid == '31S421'

make a new array from a nested object using Lodash

Here is my data
[
{
"properties": {
"key": {
"data": "companya data",
"company": "Company A"
}
},
"uniqueId" : 1
},
{
"properties": {
"key": {
"data": "companyb data",
"company": "Company B"
}
},
"uniqueId" : 2
},
{
"properties": {
"key": {
"data": "companyc data",
"company": "Company C"
}
},
"uniqueId" : 3
}
]
The format I need for my typeahead directive is below. I was trying to figure out the other post I made but still couldn't make it work. The best was to just make the nested collection as a simple collection of object.
[
{
"uniqueId" : 1,
"data": "companya data"
},
{
"uniqueId" : 2,
"data": "companyb data"
},
{
"uniqueId" : 3,
"data": "companyc data"
}
]
I got it!
console.log(
_(jsonData).map(function(obj) {
return {
d : obj.properties.key.data,
id : obj.uniqueId
}
})
.value()
);
You do not have to use the chaining feature of lodash as long as you are only performing one operation. You can simply use:
_.map(jsonData, function(obj) {
return {
d : obj.properties.key.data,
id : obj.uniqueId
}
});