I have a JSON file:
{
"user": "John",
"first": "John",
"last": "Wilson",
"updated": "2013-02-17",
"generated_at": "2013-02-13",
"version": 1.1,
}
I want to use this as the data file for my Ruby test and want to access the data in this file. I am doing the data verification as:
application[data_first]. should eq 'John'
I want to refer to the expected data from the JSON file using something like:
application[data_first]. should eq JSON_file[first]
Assuming the JSON file's name is "JSON_file", I also have added require 'JSON_file' at the top of my test script.
How do I access the data from JSON file?
Related
I want my output to be as below:-
{
"EVENT": "start",
"NAME": "schedule-rule-start",
"ECS_CLUSTER": "gps-aws-infra-ecs-cluster",
"ECS_SERVICE_NAME": [
“abc”,
"def"
],
"DESIRED_COUNT": "1",
"REGION": “eu-central-1"
}
My Input in yaml is as below:
Input: !Sub '{"EVENT": "${self:custom.input.StartECSServiceRule.Event}", "NAME": "${self:custom.input.StartECSServiceRule.Name}", "ECS_CLUSTER": "${self:custom.input.StartECSServiceRule.ClusterName}", "ECS_SERVICE_NAME": "${self:custom.input.StartECSServiceRule.ECSServiceName}","DESIRED_COUNT": "${self:custom.input.StartECSServiceRule.DesiredCount}","REGION": "${self:provider.region}"}'
I want the generated cloudformation json as below:-
“{\“EVENT\“:\“start\“,\“NAME\“:\“schedule-rule-start\“,\“ECS_CLUSTER\“:\“gps-aws-infra-ecs-cluster\“,\“ECS_SERVICE_NAME\“:["abc","def"],\“DESIRED_COUNT\“:\“1\“,\“REGION\“:\“eu-central-1\“}”
Need help on how should i provide the value of "ECSServiceName" as below doesnot work in the Input I gave in the yaml file???
"${self:custom.input.StartECSServiceRule.ECSServiceName}
Please help to correct my yaml file Input. Right now it comes as string , i want it to come as array.
I've a file containing hundreds of object and value combination like below manner. I want to get input from user as object name & numeric value and return that associated value.
Object cefcFRUPowerOperStatus
Type PowerOperType
1:offEnvOther
2:on
3:offAdmin
4:offDenied
5:offEnvPower
6:offEnvTemp
Object cefcModuleOperStatus
Type ModuleOperType
1:unknown
2:ok
3:disabled
4:okButDiagFailed
5:boot
6:selfTest
E.g. - input -
objectName = 'cefcModuleOperStatus'
TypeNumber = '4'
Return - 'okButDiagFailed'
I am not aware of Ruby and get this done to help my peer. So please excuse if this is a novice question.
Note:- I've to create the file so with any file format it would be a great help.
If like you say you have control over creating the original data file, then creating it in json format would make accessing it trivial.
Here is a repl.it of complete working example. Just select the main.rb file and hit run!
For example if you create json file like:
data.json
{
"cefcFRUPowerOperStatus": {
"type": "PowerOperType",
"status": {
"1": "offEnvOther",
"2": "on",
"3": "offAdmin",
"4": "offDenied",
"5": "offEnvPower",
"6": "offEnvTemp"
}
},
"cefcModuleOperStatus": {
"type": "ModuleOperType",
"status": {
"1": "unknown",
"2": "ok",
"3": "disabled",
"4": "okButDiagFailed",
"5": "boot",
"6": "selfTest"
}
}
}
Then parsing it and accessing it in Ruby is as simple as :
require 'json'
file = File.read('data.json')
data = JSON.parse(file)
#accessing this data is simple now:
puts data["cefcModuleOperStatus"]["status"]["4"]
# > okButDiagFailed
Note: that this JSON format will work if your statuses are unique. If they are not, you can still use this way, but you will need to convert JSON to an array format. Let me know if this is the case and I can show you how to modify the json and ruby code for this.
Hope that helps, let me know if you have further questions about how this works.
I want to know what is the correct way to assign node['ipaddress'] OHAI attribute within the chef role. My chef role is defined in the JSON format.
{
"name": "temp_role",
"description": "This is temp role",
"json_class": "Chef::Role",
"default_attributes": {
"client_addr": #{node['ipaddress']}
},
"override_attributes": {
},
"chef_type": "role",
"run_list": [
"recipe[test::prereq]"
],
"env_run_lists": {
}
}
You cannot use ohai data in roles, JSON format or otherwise. Roles are purely static data, converted to JSON during upload even if they are using the .rb DSL. anything dynamic must live in a cookbook, in this case probably a role-pattern cookbook.
lets say I have this json data file
{
"page": {
"title": "Example Page"
},
"employers": {
"name": "Jon"
},
"employees": [
{ "name": "Mike", "nicknames": ["Superman"] },
{ "name": "Peter", "nicknames": ["Peet", "Peetee", "Peterr"] }
]
}
this data.json file exist as a file outside of the script
I have these 3 lines to read and parse it with json ruby library
data = File.read("data.json")
obj = JSON.parse(data)
puts obj.values
in my terminal it comes out to be like this
{"title"=>"Example Page"}
{"name"=>"Jon"}
{"name"=>"Mike", "nicknames"=>["Superman"]}
{"name"=>"Peter", "nicknames"=>["Peet", "Peetee", "Peterr"]}
what happened to employers and employees? now I have the same key or name in this case. Its difficult for me to grab the values to use them.
Employers and employees are the keys for primary hash, and you requested values, that's why you get what you get. Try putting obj .
Using the v2 of the box api, I use the folder items request to get information on files in a folder: http://developers.box.com/docs/#folders-retrieve-a-folders-items
I'm looking at trying to parse the response data. Any ideas how I can do this in bash to easily find a file in the user's account? I would like to find the name of the file where I can get the ID of the file as well.
response looks something like this:
{
"total_count": 25,
"entries": [
{
"type": "file",
"id": "531117507",
"sequence_id": "0",
"etag": "53a93ebcbbe5686415835a1e4f4fff5efea039dc",
"name": "agile-web-development-with-rails_b10_0.pdf"
},
{
"type": "file",
"id": "1625774972",
"sequence_id": "0",
"etag": "32dd8433249b1a59019c465f61aa017f35ec9654",
"name": "Continuous Delivery.pdf"
},
{ ...
For bash, you can use sed or awk. Look at Parsing JSON with Unix tools.
Also if you can use a programming language, then python can be your fastest option. it has a nice module json http://docs.python.org/library/json.html. It has a simple decode API which will give a dict as the output
Then
import json
response_dict = json.loads(your_response)
I recommend using jq for parsing/munging json in bash. It is WAY better than trying to use sed or awk to parse it.