0

I have successfully used the include directive with HOCON before. However, my current project requires that our config files be defined in JSON for client specified reasons. This is fine, but I also need to factor out common elements of these configs into separate 'base' config files that contain the common definitions.

I have tried the syntax

{ "include":  "some-common-file.json"}

But this did not work out for me.

A sample program is shown below. The result of looking at the variable 'config' in the debugger showed that the key 'paste' had the expected value 'monkey'.

However, there was also a key 'include' with the value of 'foo.json'. The contents of foo.json were not brought in.

There must be some way to do this (I hope!)
If you can help shed light on 'how to', I'd be most grateful !

Sample Program

import com.typesafe.config.{Config, ConfigBeanFactory, ConfigFactory}
import com.typesafe.scalalogging.LazyLogging
import org.testng.annotations.Test

class ConfigTest extends LazyLogging {

  @Test def testJson(): Unit = {
    val config: Config = ConfigFactory.load("bar.json")
    System.out.println("config:" + config)
  }
}



// bar.json
{
    "include":  "foo.json",
    "paste": "monkey"
}

// foo.json
{"testRules":
  {
  "foo": "Steve",
  "bar": 30
  }
}

Note: i tried one of the suggested answers -- much appreciated -- but it didn't do the trick. Namely, this approach:

  #include "foo.json",

recommended by @newt -- for me this resulted in the following:

bar.json @ file:/project/foo/build/resources/test/bar.json: 2: Expecting close brace } or a field name here, got '#' (Reserved character '#' is not allowed outside quotes)
com.typesafe.config.ConfigException$Parse: bar.json @ file:/project/foo/build/resources/test/bar.json: 2: Expecting close brace } or a field name here, got '#' (Reserved character '#' is not allowed outside quotes)
        at com.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseError(ConfigDocumentParser.java:201)

Note2: This syntax:

  { "include":  "some-common-file.json"}

resulted in this error:

 Token not allowed in valid JSON: 'include'

Thanks to newt for the revision to the original answer, but I think the documentation referenced in the revised answer is for HOCON, not for Json. At this point, I'd give my left leg for a good working example, and I will up the bounty.

Chris Bedford
  • 2,560
  • 3
  • 28
  • 60
  • Sorry about the confusion, I have updated my answer. Please try that and let me know :) – newt Dec 25 '21 at 04:10

1 Answers1

0

HOCON documentation specifies that an unquoted include followed by a file path holds special meaning.

Bar.json:

{ include file("path/to/foo.json")
}

NOTE: Make sure that substitutions in foo.json are relative to the app's root configuration

newt
  • 321
  • 2
  • 8
  • thanks ! i did try.. regrettably no luck.. added notes on my observed results in original post. – Chris Bedford Dec 24 '21 at 00:35
  • Hey, @newt Thanks again.. I tried the revised answer but with no luck. Added comments to the original post. – Chris Bedford Dec 31 '21 at 06:39
  • what's the error, could you share? – newt Dec 31 '21 at 07:45
  • Hi.. i updated the comments w/ results of trying revised answer. in a nutshell -> " Token not allowed in valid JSON: 'include'". I am re-bountying the question. if you can provide a working example it is all yours.. along w/ my sincere thanks. – Chris Bedford Jan 03 '22 at 01:56