Json1:
{
"Name1" : "Value1",
"Name2" : "<%= Value2 %>"
}
Value2 is calling a method which reads json2 and gives an output.
ERB.new(File.read("json1.json")).result
gives me the output of second json file and not the json1.
I can not figure out how else can I replace the value 2 with the output of second json. Is there a way I can pass the value of already evaluated json for Value2?
This shows your problem:
ERB.new( "foo bar <%= ERB.new( 'baz' ).result %>" ).result
=> "baz"
This is nothing to do with JSON. It's because ERB does not nest automatically, due to how the template is evaluated. It appends to a variable called :_erbout
, and different ERB objects will use the same variable. It's fine when you want to build up a structure sequentially, but not so great for nested includes.
You can fix your problem by telling ERB to use a different named variable when generating output:
ERB.new( "foo bar <%= ERB.new( 'baz', nil, nil, :_erbout2 ).result %>", nil, nil, :_erbout1 ).result
=> "foo bar baz"
The code is starting to look ugly so you might want to abstract it (especially if you don't know in advance how deep the nesting will go, so you'll want to generate the variable names)