0

Is it possible to have a JSON file with variable something like below? This will be processed by python.

ver_file:

{
    "version":{
        "minor": 10,
        "major": 1.0.0.$minor
    }
}

so can I read it as below:

import json
with open(ver_file,"r") as vf:
    ver_config=json.load(vf)

 print(ver_config["version"]["major"])# I want this to print 1.0.0.10

I know I can concatenate major and minor with a dot, but I am working on a bigger problem where the variable usage can avoid duplicate info in json file and avoid concatenations in the python script.

1
  • No, it is not possible with the default JSON parser, but you could extend it yourself probably. Although this might get a bit tricky. Commented Jun 19, 2018 at 17:18

1 Answer 1

1

This is not a feature of JSON.

There are various "JSON template" formats that provide functionality similar to this on top of JSON, and some of them have Python libraries, like jsontemplate and JSON-template and another one I can't remember the name of that's based on JSON5 and will allow certain kinds of variable expressions that would be legal as JS source code… But whether any of them do exactly what you want (and have stable, maintained libraries that you can use out of the box), I have no idea.

Or, of course, you can design your own format and implement it yourself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.