7

I am new in work with JSON, so sorry in advance for the stupid question.

I want to write JSON with the variable in the value field. It looks like this:

def print_json(user_name):
    opened_json = open('way/to/json/file')
    tmp = json.load(opened_json)

    res = tmp(['path_to_folder'](user_name))
    print(res)

def main(user_name):
    print_json(user_name)


main('user')

It is JSON:

{"path_to_folder": "/Users/" + user_name + "/my_folder/"}

Awaiting for that output:

/Users/user/my_folder/

Please, tell me if any solution here exists.

Thanks in advance!

EDIT: My problem, that I can't add variable to JSON correctly. It marked red. Wrong syntax, when I try to concat.

10
  • 1
    res = tmp['path_to_folder'] Commented Jun 4, 2020 at 12:59
  • 1
    tmp will be a regular python dictionary containing the json object(dictionary) from your file. Commented Jun 4, 2020 at 13:00
  • 1
    I have no idea what your above comment means, sorry Commented Jun 4, 2020 at 13:04
  • 1
    {"path_to_folder": "/Users/" + user_name + "/my_folder/"} isn't valid JSON to start with. Commented Jun 4, 2020 at 13:12
  • 1
    Sorry, it's really unclear what you are asking. You have an existing json file, or you want to create a totally new json file? Commented Jun 4, 2020 at 13:12

1 Answer 1

6

What you want isn't directly possible in JSON, because it doesn't support "templating".

One solution would be to use a templating language such as Jinja to write a JSON template, then load this file without the json library and fill in the values using Jinja, and finally use json.loads to load a dictionary from your rendered string.

Your json-like file could look something like this:

{"path_to_folder": "/Users/{{ user_name }}/my_folder/"}

Your Python code:

import json
from jinja2 import Environment, FileSystemLoader

env = Environment(
    FileSystemLoader("path/to/template")
)
template = env.get_template("template_filename.json")

def print_json(username):
    return json.loads(
        template.render(user_name=username)
    )
...

In fact, if this is a simple one-time thing, it might even be better to use Python's built-in templating. I would recommend old-style formatting in the case of JSON, because otherwise you'll have to escape a lot of braces:

JSON file:

{"path_to_folder": "/Users/%(user_name)s/my_folder/"}

"Rendering":

with open("path/to/json") as f:
    rendered = json.loads(f.read() % {"user_name": username})
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! I though that it works like usual concat for string...
TypeError: no loader for this environment specified. Not sure, but maybe I made a mistake in code.
You'll have to give the Environment object some arguments, see this. I'll change it in the answer.
The final editing works fine. Thank you! Last question: my JSON consists of many rows and now it prints me all of them. How can I call specific value? Where I should add path to it?

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.