1

I'm new to python and programming in general, asking for help: What to run in python code to print this data in multiple JSON files (300 files) that every file change the "name" line from #1 to #300 and the JSON files names will be like from 1.json to 300.json is it possible?

{
  "description": "#", 
  "image": "#", 
  "name": "#1",
  "attributes": [
    {
      "trait_type": "#", 
      "value": "#"
    } 
    ] 
}
5
  • Of course it's possible. Show us your code. Commented Sep 20, 2021 at 3:36
  • This is the code that i want to it to appear in the json files : { "description": "#", "image": "#", "name": "#1", "attributes": [ { "trait_type": "#", "value": "#" } ] } Commented Sep 20, 2021 at 3:40
  • Sure, it’s possible. Use an f-string (or the like) to fill in the name in the JSON snippet and then print it using the standard Python file API, e.g. in a loop over a range(1, 301) or the like. As a side note, it would be way easier to generate the files directly in Bash; simple file operations (redirections) are way shorter there than in Python, e.g. for ((i = 1; i <= 300; ++i)); do printf "${JSON_TEMPLATE}" "$i" > "${i}.json"; done. What JSON_TEMPLATE should be is probably obvious (and it would have %d somewhere in it to fill in the number). Commented Sep 20, 2021 at 4:05
  • thank you @AndrejPodzimek I get the idea but I can't understand the way to do it like how json file will be created and where to put these codes, can you please show me the code if it's not too long? Commented Sep 20, 2021 at 4:34
  • @Anis That's simply a Bash snippet / one-liner. Put it in a script file or run it directly, as you prefer. The JSON_TEMPLATE variable should have your JSON stuff assigned to it, verbatim, so e.g. JSON_TEMPLATE='{ ... name: "#%d", ... }' etc. (You can have newlines in quotes in Bash, no problem at all. (Or in Python you would use the ''' / """ string literal type if you decide to go with Python. (Or, even better, a JSON-aware library, as someone already pointed out.))) Commented Sep 20, 2021 at 4:48

1 Answer 1

1

try this:

# library to work with json stuff
import json

# you can create dict first represent the json
json_dict = {
  "description": "#", 
  "image": "#", 
  "name": "#1",
  "attributes": [
    {
      "trait_type": "#", 
      "value": "#"
    } 
    ] 
}

# define numbers of file you want to create
n = 300
for i in range(1, n+1):
    # change the "name" atribute for every iteration
    # you can use f-string method
    json_dict["name"] = f"#{i}"

    # save the dict to file
    # again, use the f-string to name the file
    with open(f"{i}.json", 'w') as json_file:
        #json.dump() method save dict json to file
        json.dump(json_dict, json_file)
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.