#๐Ÿ”’ JSON

26 messages ยท Page 1 of 1 (latest)

fresh elbow
#

Hi have an issue wiht my code. I was abel to run successfully run ' json_str' .However, when i want to load it, it does not work.

tawny caveBOT
#

@fresh elbow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mystic gazelle
#

does the if statement evaluate?

fresh elbow
#

yes

tawny caveBOT
mystic gazelle
#

!paste

tawny caveBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

mystic gazelle
#

paste here

fresh elbow
mystic gazelle
#

while in if check ur checking for user == 2

#

!e

import json
user = 2
class Car:
    #Defining the class car that takes paramters\
    #registration number, year of production, passenger , vehicle mass
    def __init__(self,number, year, passenger, mass):
        self.registration_number= number
        self.year_of_production= year
        self.passenger = passenger
        self.vehicle_mass= mass

def decod(obj):
    return Car(obj['registration_number'], obj['year_of_production'],obj['passenger'],obj['vehicle_mass'] )
#create new car object
MyCar=Car(number="ABC1234",year=2024,passenger=True, mass=19432 )



#produce a json string descring the vehicle
try:
    json_str= json.dumps(MyCar, default= lambda o: o.__dict__)
except TypeError:
    print(f'Object {MyCar.__class__.__name__} is not serializable')
else:
    print(f'The resulting JSON string is :\n {json_str}')

if user ==2:
    json_decoded= json.loads(json_str, object_hook = decod)
    print(json_decoded.__dict__)
tawny caveBOT
mystic gazelle
#

yep

#

wrong key in decode and user = 2 u need to fix

fresh elbow
mystic gazelle
#

in ur case
if user == 2 was false

#

u set user to 1 in ur input

#

so it wasn't evaluating at all

#

and in decod function u had obj['mass'] which is wrong it should be obj['vehicle_mass']

fresh elbow
#

thANKS A LOT

tawny caveBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.