Using custom field names for json encoding in python - python

I have a python script that should generate a json file in a specific structure.
So to achieve that, my plan was to create my entity data classes that represents the structure of this json file, construct them, do all the magic needed for generation, and dump my object into a json file with json.dumps(my_object).
Now my problem is that this json structure have fields like weird-field:. since I can't use the "dash" symbol in my dataclass because of python syntax, I can't create my entity class that represents my json structure. (This json file will be used by another system, so I have no way to change the structure.)
Right now I worked around it by using wrong field names like weird_field that are accepted by python, then after encoding them to json I manually replaced those wrong field names in the json string.
I wonder if there is a better way to do it. In java you can just use a special annotation on those fields in your class to say "Hey Jackson, use this string for encoding instead of the name of the class' field". What's the python way to do the same?
In code what I'd like to do is:
#dataclass
class MyClass:
weird-field: int = 0 # syntax error here
json_obj = MyClass()
json_obj.weird-field = 621
print(json.dumps(json_obj))
I'd like to get
{
weird-field: 621
}
But it crashes since '-' can't be used for field name in python.

Use dictionaries.
json_obj = dict()
json_obj['weird-field'] = 621
my_weird_value = json_obj['weird-field'] # returns 621
print(json.dumps(json_obj))

Related

Is there a way to search for a string and copy text in front until it reaches a comma?

I am new to python and wanted to store the recentAveragePrice inside a variable (from a string like this one)
{"assetStock":null,"sales":250694,"numberRemaining":null,"recentAveragePrice":731,"originalPrice":null,"priceDataPoints":[{"value":661,"date":"2022-08-11T05:00:00Z"},{"value":592,"date":"2022-08-10T05:00:00Z"},{"value":443,"date":"2022-08-09T05:00:00Z"}],"volumeDataPoints":[{"value":155,"date":"2022-08-11T05:00:00Z"},{"value":4595,"date":"2022-08-10T05:00:00Z"},{"value":12675,"date":"2022-08-09T05:00:00Z"},{"value":22179,"date":"2022-08-08T05:00:00Z"},{"value":15181,"date":"2022-08-07T05:00:00Z"},{"value":14541,"date":"2022-08-06T05:00:00Z"},{"value":15310,"date":"2022-08-05T05:00:00Z"},{"value":14146,"date":"2022-08-04T05:00:00Z"},{"value":13083,"date":"2022-08-03T05:00:00Z"},{"value":14460,"date":"2022-08-02T05:00:00Z"},{"value":16809,"date":"2022-08-01T05:00:00Z"},{"value":17571,"date":"2022-07-31T05:00:00Z"},{"value":23907,"date":"2022-07-30T05:00:00Z"},{"value":39007,"date":"2022-07-29T05:00:00Z"},{"value":38823,"date":"2022-07-28T05:00:00Z"}]}
My current solution is this:
var = sampleStr[78] + sampleStr[79] + sampleStr[80]
It works for the current string but if the recentAveragePrice was above 999 it would stop working and i was wondering if instead of getting a fixed number i could search for it inside the string.
Your replit code shows that you're acquiring JSON data from some website. Here's an example based on the URL that you're using. It shows how you check the response status, acquire the JSON data as a Python dictionary then print a value associated with a particular key. If the key is missing, it will print None:
import requests
(r := requests.get('https://economy.roblox.com/v1/assets/10159617728/resale-data')).raise_for_status()
jdata = r.json()
print(jdata.get('recentAveragePrice'))
Output:
640
Since this is json you should just be able to parse it and access recentAveragePrice:
import json
sample_string = '''{"assetStock":null,"sales":250694,"numberRemaining":null,"recentAveragePrice":731,"originalPrice":null,"priceDataPoints":[{"value":661,"date":"2022-08-11T05:00:00Z"},{"value":592,"date":"2022-08-10T05:00:00Z"},{"value":443,"date":"2022-08-09T05:00:00Z"}],"volumeDataPoints":[{"value":155,"date":"2022-08-11T05:00:00Z"},{"value":4595,"date":"2022-08-10T05:00:00Z"},{"value":12675,"date":"2022-08-09T05:00:00Z"},{"value":22179,"date":"2022-08-08T05:00:00Z"},{"value":15181,"date":"2022-08-07T05:00:00Z"},{"value":14541,"date":"2022-08-06T05:00:00Z"},{"value":15310,"date":"2022-08-05T05:00:00Z"},{"value":14146,"date":"2022-08-04T05:00:00Z"},{"value":13083,"date":"2022-08-03T05:00:00Z"},{"value":14460,"date":"2022-08-02T05:00:00Z"},{"value":16809,"date":"2022-08-01T05:00:00Z"},{"value":17571,"date":"2022-07-31T05:00:00Z"},{"value":23907,"date":"2022-07-30T05:00:00Z"},{"value":39007,"date":"2022-07-29T05:00:00Z"},{"value":38823,"date":"2022-07-28T05:00:00Z"}]}'''
data = json.loads(sample_string)
recent_price = data['recentAveragePrice']
print(recent_price)
outputs:
731
Your data is in a popular format called JSON (JavaScript Object Notation). It's commonly used to exchange data between different systems like a server and a client, or a Python program and JavaScript program.
Now Python doesn't use JSON per-se, but it has a data type called a dictionary that behaves very similarly to JSON. You can access elements of a dictionary as simply as:
print(my_dictionary["recentAveragePrice"])
Python has a built-in library meant specifically to handle JSON data, and it includes a function called loads() that can convert a string into a Python dictionary. We'll use that.
Finally, putting all that together, here is a more robust program to help parse your string and pick out the data you need. Dictionaries can do a lot more cool stuff, so make sure you take a look at the links above.
# import the JSON library
# specifically, we import the `loads()` function, which will convert a JSON string into a Python object
from json import loads
# let's store your string in a variable
original_string = """
{"assetStock":null,"sales":250694,"numberRemaining":null,"recentAveragePrice":731,"originalPrice":null,"priceDataPoints":[{"value":661,"date":"2022-08-11T05:00:00Z"},{"value":592,"date":"2022-08-10T05:00:00Z"},{"value":443,"date":"2022-08-09T05:00:00Z"}],"volumeDataPoints":[{"value":155,"date":"2022-08-11T05:00:00Z"},{"value":4595,"date":"2022-08-10T05:00:00Z"},{"value":12675,"date":"2022-08-09T05:00:00Z"},{"value":22179,"date":"2022-08-08T05:00:00Z"},{"value":15181,"date":"2022-08-07T05:00:00Z"},{"value":14541,"date":"2022-08-06T05:00:00Z"},{"value":15310,"date":"2022-08-05T05:00:00Z"},{"value":14146,"date":"2022-08-04T05:00:00Z"},{"value":13083,"date":"2022-08-03T05:00:00Z"},{"value":14460,"date":"2022-08-02T05:00:00Z"},{"value":16809,"date":"2022-08-01T05:00:00Z"},{"value":17571,"date":"2022-07-31T05:00:00Z"},{"value":23907,"date":"2022-07-30T05:00:00Z"},{"value":39007,"date":"2022-07-29T05:00:00Z"},{"value":38823,"date":"2022-07-28T05:00:00Z"}]}
"""
# convert the string into a dictionary object
dictionary_object = loads(original_string)
# access the element you need
print(dictionary_object["recentAveragePrice"])
Output upon running this program:
$ python exp.py
731

How to extract only wanted property from JSON object

When I run the code:
import requests
import json
def get_fact():
catFact = requests.get("https://catfact.ninja/fact?max_length=140")
json_data = json.loads(catFact.text)
return json_data
print(get_fact())
The output is like
{'fact': "Cats are the world's most popular pets, outnumbering dogs by as many as three to one", 'length': 84}
However I just want the fact.
How do I get rid of the 'fact:' at the front and 'length:' at the back?
What you want is to access the key in the python dict you made with the json.loads call. We actually don't need the json library as requests can read and deserialize JSON itself.
This code also checks if the response was OK and fails with informative error message. It follows PEP 20 – The Zen of Python.
import requests
def get_fact():
# Get the facts dictionary in a JSON serialized form.
cat_fact_response = requests.get("https://catfact.ninja/fact?max_length=140")
# Let the response raise the exception if something bad happened to the cat facts server connection.
cat_fact_response.raise_for_status()
# Deserialize the json (make a Python dict from the text we got). requests can do that on it's own:
cat_fact_dict = cat_fact_response.json()
# Access the fact from the json from the dictionary
return cat_fact_dict['fact']
print(get_fact())
When called you get following output as wanted:
# python3 script.py
The cat's tail is used to maintain balance.
Short answer:
you need to use either get_fact()['fact'] or get_fact().get('fact'). The former will throw an exception if fact doesn't exist whereas the latter will return None.
Why:
In your code sample you fetch some json data, and then print out the entire bit of json. When you parse json, the output is a key/value map called a dictionary (or map or object in other languages). The dictionary in this case contains two keys: fact and length. If you only one want of the values, then you need to tell python that you want only a single value -- fact in this case.
Remember though: this wouldn't apply to every json object you read. Not every one is going to have a fact key.
What you are returning in get_fact is a complete JSON object which you are then printing.
To get just its property fact (without the length) use a reference to that key or property like:
return json_data["fact"]
Below is also a link to a tutorial on using JSON in Python:
w3schools: Python JSON
To extract fact field from the response, use:
import requests
import json
def get_fact():
catFact = requests.get("https://catfact.ninja/fact?max_length=140")
json_data = json.loads(catFact.text)
return json_data['fact'] # <- HERE
print(get_fact())
Output:
Cats have "nine lives" thanks to a flexible spine and powerful leg and back muscles
Note: you don't need json module here, use json() method of Response instance returned by requests:
import requests
def get_fact():
catFact = requests.get("https://catfact.ninja/fact?max_length=140").json()
return catFact['fact']
print(get_fact())

Library for converting between python objects and JSON data structures

I need to work in an environment where the server has data objects cached in memory, and some or all of them needs to be sent over a websocket to a client. The conversion between the objects and the data structures is very straingforward. For example, here is a TypeScript definition of a data transfer object:
export interface IFieldStruct {
field_name: string;
type: string;
displaylabel: string;
notnull: boolean;
}
The corresponding Python objects looks like this:
class FieldStuct:
def __init__(field_name: str, type: str, displaylabel: str, notnull: bool):
self.field_name = field_name
self.field_name = field_name
self.displaylabel = displaylabel
self.notnull = notnull
Actually, the Python objects on the server side are smarter than that. They also have methods, and they also have some attributes that need not to be exported to JSON. Some of their attributes can be lists and dictionaries containing other smart objects.
Here is the problem. I would like to take advantage of code completion and code inspection in my Python IDE (pycharm). So I don't want to store this data as a data structure in Python. But I also want to be able to convert and send these objects easily.
I know that I could write my own serializer/deserializer for this. But there will be hundreds of data object classes, and I do not want to write a serializer manually. I wonder if there is good a library that already does this for me with object introspection? I do not want to reinvent the wheel. There are too many libs on PyPi, and I'm not able to find the right one. I'm not asking for opinions, I'm just asking for a list of the most popular libs that can help me in the conversion.
Pickle is one of the most popular (de)serializations libs out there, if not the most popular.
https://docs.python.org/3/library/pickle.html

Overflow error when reading json file

I am trying to read a json which includes a number of tweets, but I get the following error.
OverflowError: int too large to convert
The script filters multiple json files to get specific tweets, and it crashes when reaching to a specific json.
The line that creates the error is this one :
df_temp = pd.read_json(path_or_buf=json_path, lines=True)
Here is the error in the cmd
Just store the user id as a String, and treat it like it is one (this is actually what you should do when dealing with this kind of ids). If you can't change the json input format, you can always parse it like a string before parsing it like a json object, and add the quotes to the id code, using for instance regexes: Regex in python.
I don't know with which library you are parsing the json, but maybe also implicit casting will work: either try the "getString" method on the number instead of the "getInt" method, or force python to treat the object like a string, with something like x = "" + json.getId()
Python is pretty loose on typing and may let you do it.

Converting a String to Type Object - Python

I have created a DataStore, and the type of fields in the data store has been saved in json format.
Something like
{"SchemaName": "Caztor", "FieldType": {"cain": "<type 'float'>"}}
Now when i load the json using json.load , the type of the field cain is being loaded as Unicode. Is there any pre defined methods available in python which can be used to typecast this String to Type object. Ofcourse I can make a function that would handle and solve the problem. But if there is already a solution to this in the python library then it would save a bit of overhead and might be more efficient in handling the problem than I might do.
You can use the module pickle to solve the typecast. this module can serialize the instances into a string
The Documentation is present on https://docs.python.org/2/library/pickle.html
Example
import pickle
schema = {"SchemaName": "Caztor", "FieldType": {"cain": float}}
result = pickle.dumps(schema) # Will result a string
To deserialize use:
result2 = pickle.loads(result) # Will result a dictionary with typecast
There is no solution. JSON is a universal format across all languages and it doesn't make sense for it to be able to store Python classes. You need to define a fixed way to go back and forth between strings and types, which includes determining which types are acceptable. Here's an example:
class Example(object):
pass
json_types = [int, float, str, Example]
string_to_type_dict = {t.__name__: t for t in json_types}
assert len(string_to_type_dict) == len(json_types)
def string_to_type(s):
return string_to_type_dict[s]
def type_to_string(t):
return t.__name__

Categories