Change format of json output [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The following method gives me:
ICD1 = []
def parse_kapitel(node)
ICD1 << {von: node.css('~ von_icd_code')[0]['V'],
bis: node.css('~ bis_icd_code')[0]['V'],
bezeichnung: node.css('~ bezeichnung')[0]['V']}
end
File.write('Icd1.json', ICD1)
an output that looks something like this:
[{:von=>"A00", :bis=>"B99", :bezeichnung=>"Bestim.....
But I would like an output that looks like this:
[{"von":"A00", "bis":"B99", "bezeichnung":"Bestim.....
How can I achieve this in an easy ruby way?

Do as below using Generating JSON :
require 'json'
[{ :von=>"A00", :bis=>"B99", :bezeichnung=>"Bestim" }].map(&:to_json)
# => ["{\"von\":\"A00\",\"bis\":\"B99\",\"bezeichnung\":\"Bestim\"}"]

Related

Golang: GetRawData() of ginCtx gets empty value "len:0, cap:512" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
I'm trying to get the body of the request but the method for get it of gin Context does not brings anything.
my endpoint: foo.com/root?q=123
in the controller
bodyInBytes:=ctx.GetRawData()
body is equal to "" len:0, cap:512
get the body of request
my endpoint was: foo.com/root/?country=123
in the controller :
bodyInBytes:=ctx.GetRawData()// body = "" len:0, cap:512
but
country := ctx.Param("country")//country = 123
solution:
change the slash at the end of the endpoint
my endpoint now is: foo.com/root?country=123. (Note I removed the slash )
in the controller :
bodyInBytes:=ctx.GetRawData()// body = "{"test":"Test",} " len:64, cap:512
and
country := ctx.Param("country")//country = 123
by some reason gin don't bring the body when the url has a slash at the end of de endpoint "/"

Why it is showing `missing ',' before newline in composite literal` [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Why my vscode is showing this error
missing ',' before newline in composite literal
My code:
if title == "" && desc != "" {
msgs = database.UpdateNotification(&Notify, map[string]interface{}{
"Description": changedDesc,
}); msgs != nil {
log.Info("error while deleting notification")
}```
So you want to able to change the variable names dynamically?
There isnt any way of doing this. I suggest if you really need somthing like that you would use a Map.
Data map[String]String
Im not exactly sure what you need this for, or if this helps.

How do I output name and value of a variable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am new to VB Script and have a question, how do I assign a Name to the Variables I have input so that they display on the screen?
For example, Once I have input a name I would like the final information box to show the Name value as:
Name: <entered value>
I understand how to assign the 'entered value' but not the 'Name' itself..?
name=inputbox("What is your name?", "Personal Details")
address=inputbox("Please enter your Full Address inc. Post Code", "Address")
tele=inputbox("Please enter your telephone number", "Telephone Number")
msgbox name &vbLf&vbLf& address &vbLf&vbLf& tele, 1, "New Customer Details"
The question isn't very clear so it's quite possible I've misunderstood, but if you just want to know how to add in a static string to your output, then you just add it as "Name: " &.
So combined with your code it would be something like:
msgbox "Name: " &name &vbLf&vbLf& address &vbLf&vbLf& tele, 1, "New Customer Details"

How do I convert ~24000 product titles into URL keys? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array with ~24000 products. The hash will be saved as a CSV and uploaded to a Shopify shop using the products import method.
When I manually create a single product, the product url key/handle is automatically generated based on the product title. When using the products import method (CSV), I'll have to specify it myself.
How do I convert the titles into product url keys?
Example:
title_1 = "AH Verse frietaardappelen"
url_key_1 = "ah-verse-frietaardappelen"
title_2 = "Lay's Sensations red sweet paprika"
url_key_2 = "lay-s-sensations-red-sweet-paprika"
I'm currently using:
<title>.downcase.gsub(' ','-').gsub("'", '-')
but this doesn't remove %, $, &, / etc. from the title. I want to make sure the url key/product handle is as clean as possible.
There must be a better way to do this, what could I try next?
There's a (private) to_handle method in Shopify's Liquid gem:
def to_handle(str)
result = str.dup
result.downcase!
result.delete!("'\"()[]")
result.gsub!(/\W+/, '-')
result.gsub!(/-+\z/, '') if result[-1] == '-'
result.gsub!(/\A-+/, '') if result[0] == '-'
result
end
Example:
to_handle("AH Verse frietaardappelen")
#=> "ah-verse-frietaardappelen"
to_handle("Lay's Sensations red sweet paprika")
#=> "lays-sensations-red-sweet-paprika"
Have a look at the gem String Urlize, it may help you write a script to do this.
I would suggest you to use Rails ActiveSupport::Inflector#parameterize solution - http://apidock.com/rails/ActiveSupport/Inflector/parameterize
It handles a lot of edge cases and should work well for you.
The best thing is to use parameterize method:
title_1 = "AH Verse $frietaardappelen".parameterize
Output: "ah-verse-frietaardappelen"
title_2 = "Lay's Sensations red %sweet paprika".parameterize
output: "lay-s-sensations-red-sweet-paprika"

Parsing gherkin into json [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am sure this is a very stupid question but I cannot get my head around it.
I have following ruby code:
sample_test = "Feature: Some terse yet descriptive text of what is desired
Textual description of the business value of this feature
Business rules that govern the scope of the feature
Any additional information that will make the feature easier to understand
Scenario: Some determinable business situation
Given some precondition
And some other precondition
When some action by the actor
And some other action
And yet another action
Then some testable outcome is achieved
And something else we can check happens too"
io = StringIO.new
pretty_formatter = Gherkin::Formatter::PrettyFormatter.new(io, true, false)
json_formatter = Gherkin::Formatter::JSONFormatter.new(io)
parser = Gherkin::Parser::Parser.new(json_formatter)
result = parser.parse(sample_test, '', 0)
This returns True.
But I want to get a JSON formatted result. What should I use to get JSON output of all the steps?
ok, I found it. This official example works pretty well:
require 'gherkin/parser/parser'
require 'gherkin/formatter/json_formatter'
require 'stringio'
require 'multi_json'
# This example reads a couple of features and outputs them as JSON.
io = StringIO.new
formatter = Gherkin::Formatter::JSONFormatter.new(io)
parser = Gherkin::Parser::Parser.new(formatter)
sources = ["features/native_lexer.feature", "features/escaped_pipes.feature"]
sources.each do |s|
path = File.expand_path(File.dirname(__FILE__) + '/../' + s)
parser.parse(IO.read(path), path, 0)
end
formatter.done
puts MultiJson.dump(MultiJson.load(io.string), :pretty => true)

Resources