[英]hyphen in key for json using Jbuilder instead of underscore
I'm trying to make this json by jbuilder 我正在尝试通过jbuilder制作此json
{
"query": {
"bool": {
"must": [
{ "match": { "source-id":2}}
]
}
}
}
and here is my code 这是我的代码
query = Jbuilder.encode do |json|
json.query do
json.bool do
json.must do
json.match do
json.source-id source.id
end
end
end
end
end
I got this error message 我收到此错误消息
syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' json.source-id source.id ^ from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/console.rb:65:in
start' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/console_helper.rb:9:in
start' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:78:inconsole' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:49:in
run_command!' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands.rb:18:in<top (required)>' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in
require' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:inblock in require' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in
load_dependency' from语法错误,意外的tIDENTIFIER,期望来自/Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/的keyword_do或'{'或'('json.source-id source.id ^ lib / rails / commands / console.rb:65:
start' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/console_helper.rb:9:in
从'/Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:78:inconsole' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:49:in
run_command!中来自/Users/amir/.rvm/gems/ruby-2.3 .0 / gems / railties-5.0.2 / lib / rails / commands.rb:18:在<top (required)>' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in
中的<top (required)>' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in
需求中来自/Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb: 293:inblock in require' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in
/Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in load_dependency'来自 /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:inrequire' from /Users/amir/source/innovate/self_driving_ideas/bin/rails:9:in
' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:inload' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in
block in load' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:inload_dependency' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in
load' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:incall' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in
call' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:191:inblock in serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in
require' from /Users/amir/source/innovate/self_driving_ideas/bin/rails:9:in
/Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:inrequire' from /Users/amir/source/innovate/self_driving_ideas/bin/rails:9:in
'来自/Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:inload' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in
:来自/Users/amir/.rvm/gems/ruby-2.3的加载块中。 0 / gems / activesupport-5.0.2 / lib / active_support / dependencies.rb:259:在load_dependency' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in
负载”从/Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:incall' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in
call' ruby-2.3.0 / gems / spring-1.7.2 / lib / spring / application.rb:191:block in serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in
block in serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in
fork' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:inserve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in
block in run' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:inloop' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in
run' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in<top (required)>' from /Users/amir/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in
require' from /Users/amir/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'block in serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in
/Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161block in serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in
serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in
中的'来自/Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:运行中的serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in
块gems / ruby-2.3.0 / gems / spring-1.7.2 / lib / spring / application.rb:125:inloop' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in
运行中/Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application/boot。 rb:19:in<top (required)>' from /Users/amir/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in
来自/Users/amir/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:“需要”
The issue is with having hyphen in source-id
key name. 问题在于
source-id
密钥名称中带有连字符。 Try doing this if source_id
works as key. 如果
source_id
用作键,请尝试执行此操作。
query = Jbuilder.encode do |json|
json.query do
json.bool do
json.must do
json.match do
json.source_id source.id
end
end
end
end
end
UPDATE:: 更新::
Otherwise you can do this to get the key formatted: 否则,您可以这样做来格式化密钥:
Jbuilder.encode do |json|
json.key_format! ->(key) { (key=="source_id") ? "source-id" : key}
json.query do
json.bool do
json.must do
json.match do
json.source_id source.id
end
end
end
end
end
To transform all strings: 要转换所有字符串:
Jbuilder.encode do |json|
json.key_format! :dasherize
json.query do
json.bool do
json.must do
json.match do
json.source_id source.id
end
end
end
end
end
Or Use set!
或使用
set!
syntax like json.set! "source-id", source.id
像
json.set! "source-id", source.id
这样的语法json.set! "source-id", source.id
json.set! "source-id", source.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.