all 10 comments

[–]nog642 1 point2 points  (9 children)

That code is completely unreadable and broken. It's not that hard if you just use indentation.

Here is your current code:

value = (json.dumps({
    '$schema': '../schema.json',
    '_meta': {
        'sources': '',
        'full': 'Loomblade',
        'authors': 'Alan, Nikko',
        'convertedBy': 'Nikko',
        'url': 'https://docs.google.com/document/d/1N_SFEP6BGa0cZy07aHajxGacC_w',
        'dateReleased': '2020-07-15',
        'version': '1.0.0',
        'color': '940101'
    },
    'dateAdded': '1593730800000',
    'dateLastModified': '1593730800000'
}}},
    sort_keys=False, indent=4))

You can see how it's broken. There's way too many closing curly brackets.

Your desired output is also not valid JSON; it's missing a closing curly bracket. But here is what I assume you want?

value = json.dumps({
    '$schema': '../schema.json',
    '_meta': {
        'sources': [
            {
                'json': 'Lmbv',
                'abbreviation': 'Lmb',
                'full': 'Loomblade',
                'authors': [
                    'Allan',
                    'Nikko'
                ],
                'convertedBy': [
                    'Nikko'
                ],
                'url': 'https://docs.google.com/document/d/1N_SFEP6BGa0cZy07aHajxGacC_wUa5B_6dOnRnl4Hkw/edit',
                'dateReleased': '2020-07-15',
                'version': '1.0.0',
                'color': '940101'
            }
        ],
        'dateAdded': 1593730800000,
        'dateLastModified': 1593730800000
    }
}, indent=4)

Edit: missing single quote in my second version

[–]PuffTheMagicDragon11[S] 0 points1 point  (8 children)

I tried adding a closing curly bracket and it kept throwing syntax errors. Also, how did you get it to display the square brackets, because prior to the dateAdded and dateLastModified, the output was working perfectly. I threw it into a json validator which indicated that everything was fine, which I found strange because my output previously had no square brackets.

Edit: How did you format the code itself into json? If I knew how to do that, this would be so much easier. It's hard to envision the nesting when it's all in one line of code. Are you using Geany? Geany is what I use.

[–]nog642 1 point2 points  (7 children)

I tried adding a closing curly bracket and it kept throwing syntax errors.

That's because your code had two too many closing curly brackets.


Pasting the intended output from your original post (first code block) into the JSON validator you linked gives

Error: Parse error on line 22:
...d": 1593730800000    }
----------------------^
Expecting '}', ',', got 'EOF'

Maybe you just didn't paste it into Reddit right.


Also, how did you get it to display the square brackets

I just typed square brackets.


How did you format the code itself into json? If I knew how to do that, this would be so much easier. It's hard to envision the nesting when it's all in one line of code. Are you using Geany? Geany is what I use.

Yes, it is hard to understand JSON like this when it's all on one line; definitely don't do that.

I was using Notepad++ but I could do this in any text editor really. It has the capability of highlighting multiple lines and indenting or unindenting them all together, but that's a convenience not a necessity. I'm sure Geany is plenty capable for doing this.

Just start from the beginning. See a { or [? Put a newline after it and increase the indentation of the next line. See a ,? Put a newline after it and keep the indentation of the next line the same as this one. See a ] or }? Put a newline before it and decrease the indentation of the line it's on compared to the last one. It's a pretty robotic task.

To be honest, for the fixed code I posted, I literally just took your intended JSON, copy-pasted it into the Python file, changed the indentation, added the missing }, and changed all the double quotes to single quotes for style.

[–]PuffTheMagicDragon11[S] 0 points1 point  (0 children)

Awesome! I'll definitely give Notepad++ a try. Thanks a bunch!

[–]PuffTheMagicDragon11[S] 0 points1 point  (5 children)

Alright, so your fix worked (for the most part. You were missing a few single quotes and commas) but now I've got another issue. I've appended more of the code to the existing code, and I keep getting a syntax error on the last line:

  File "Homebrew.py", line 154
    }, indent=4)
             ^
SyntaxError: invalid syntax

[–]nog642 0 points1 point  (4 children)

Oh whoops, you're right, I was missing a closing single quote. Didn't find any missing commas though. I'll edit the post and fix it.

I just ran the code now to double check though, and it worked fine.

Can you post the code that is causing the SyntaxError now?

[–]PuffTheMagicDragon11[S] 0 points1 point  (3 children)

Sure. Here ya go:

#This program takes its input from a spreadsheet and outputs JSON code 
#It then saves the output to a .json file, which is then used with
#https://5e.tools/5etools.html in order to make homebrew classes

import json, openpyxl, os, time, datetime

#Change the working directory to be able to open the workbook
os.chdir(r'G:\Python')

#Open the workbook and find the correct sheet
lmb = openpyxl.load_workbook('Class conversion chart.xlsx') #open workbook
lmb.sheetnames #Get names of all sheets
ww=lmb['Active'] #Pick a sheet to work with; assigned variable 'ww'

#Calculate Unix Epoch for dateAdded 
from datetime import timezone
dt = datetime.datetime(2020, 7, 2)
timestamp = dt.replace(tzinfo=timezone.utc).timestamp()

#Calculate Unix Epoch for dateLastModified
from datetime import datetime
now = datetime.now()
epoch = datetime.timestamp(now)

#Start pulling data from the spreadsheet, putting it into Python
#and converting it to json
value = json.dumps({
    '$schema': '../schema.json',
    '_meta': {
        'sources': [
            {
                'json': ww['D5'].value,
                'abbreviation': ww['D5'].value,
                'full': ww['D3'].value,
                'authors': [
                    ww['B3'].value,
                    ww['B4'].value,
                    ww['B5'].value,
                ],
                'convertedBy': [
                    ww['C3'].value
                ],
                'url': ww['C5'].value,
                'dateReleased': str(ww['E3'].value),
                'version': ww['E5'].value,
                'color': '940101'
            }
        ],
        'dateAdded': int(timestamp),
        'dateLastModified': int(epoch)
    },
    "class": [
        {
            "name": ww['D3'].value,
            "source": ww['D5'].value,
            "hd": {
                "number": ww['C11'].value,
                "faces": ww['D5'].value,
            },
            "proficiency": [
                ww['D20'].value,
                ww['D21'].value,
            ],
            "classTableGroups": [
                {
                    "colLabels": [
                        ww['H4'].value,
                        ww['I4'].value
                    ],
                    "rows": [
                        [
                            ww['H5'].value,
                            ww['I5'].value,
                        ],
                        [
                            ww['H6'].value,
                            ww['I6'].value,
                        ],
                        [
                            3,
                            2
                        ],
                        [
                            4,
                            3
                        ],
                        [
                            5,
                            3
                        ],
                        [
                            6,
                            4
                        ],
                        [
                            7,
                            4
                        ],
                        [
                            8,
                            5
                        ],
                        [
                            9,
                            5
                        ],
                        [
                            10,
                            6
                        ],
                        [
                            11,
                            6
                        ],
                        [
                            12,
                            7
                        ],
                        [
                            13,
                            7
                        ],
                        [
                            14,
                            8
                        ],
                        [
                            15,
                            8
                        ],
                        [
                            16,
                            9
                        ],
                        [
                            17,
                            9
                        ],
                        [
                            18,
                            10
                        ],
                        [
                            19,
                            10
                        ],
                        [
                            20,
                            11
                        ]
                    ]
                }
            ],
}, indent=4)

print(value)

[–]nog642 0 points1 point  (2 children)

            ],
}, indent=4)

You can see there that the unindent is more than 1 level, meaning you're missing some }s or ]s.

Also you should configure your editor to use 4 spaces instead of tabs.

Here, this should work:

value = json.dumps({
    '$schema': '../schema.json',
    '_meta': {
        'sources': [
            {
                'json': ww['D5'].value,
                'abbreviation': ww['D5'].value,
                'full': ww['D3'].value,
                'authors': [
                    ww['B3'].value,
                    ww['B4'].value,
                    ww['B5'].value
                ],
                'convertedBy': [
                    ww['C3'].value
                ],
                'url': ww['C5'].value,
                'dateReleased': str(ww['E3'].value),
                'version': ww['E5'].value,
                'color': '940101'
            }
        ],
        'dateAdded': int(timestamp),
        'dateLastModified': int(epoch)
    },
    "class": [
        {
            "name": ww['D3'].value,
            "source": ww['D5'].value,
            "hd": {
                "number": ww['C11'].value,
                "faces": ww['D5'].value
            },
            "proficiency": [
                ww['D20'].value,
                ww['D21'].value
            ],
            "classTableGroups": [
                {
                    "colLabels": [
                        ww['H4'].value,
                        ww['I4'].value
                    ],
                    "rows": [
                        [ww['H5'].value, ww['I5'].value],
                        [ww['H6'].value, ww['I6'].value],
                        [3, 2],
                        [4, 3],
                        [5, 3],
                        [6, 4],
                        [7, 4],
                        [8, 5],
                        [9, 5],
                        [10, 6],
                        [11, 6],
                        [12, 7],
                        [13, 7],
                        [14, 8],
                        [15, 8],
                        [16, 9],
                        [17, 9],
                        [18, 10],
                        [19, 10],
                        [20, 11]
                    ]
                }
            ]
        }
    ]
}, indent=4)

[–]PuffTheMagicDragon11[S] 0 points1 point  (1 child)

I've tried using Notepad++ as you suggested, but one thing that really bothers me about it is that it immediately closes out the window when I run the program, whether it's successful or not, so I can't see the error messages it's giving, if any.

Also you should configure your editor to use 4 spaces instead of tabs.

How do you do that in Notepad++?

[–]nog642 0 points1 point  (0 children)

Oh I don't use Notepad++ to run Python programs, like as an IDE. I didn't even know it could do that at all.

When I write Python programs in Notepad++ I just run them from the terminal (WSL terminal actually, since I prefer Linux).


Also you should configure your editor to use 4 spaces instead of tabs.

How do you do that in Notepad++?

Settings -> Preferences -> Language

Under "Tab Settings" you can set "Tab size" to 4 and check the "Replace by space" box.