I have the following json:
[
{
"id": 1,
"category": {
"id": 0,
"nombre": "string"
},
"nombre": "Julian"
},
{
"id": 2,
"category": {
"id": 0,
"nombre": "string"
},
"nombre": "Pedro"
},
{
"id": 3,
"category": {
"id": 0,
"nombre": "string"
},
"nombre": "Julian"
},
{
"id": 4,
"category": {
"id": 0,
"nombre": "string"
},
"nombre": "Pedro"
},
{
"id": 5,
"category": {
"id": 0,
"nombre": "string"
},
"nombre": "Maria"
}
]
And I have the following python class:
import json
id = 0
nombre = ""
class Persona(object):
def __init__(self, id, nombre):
self.id = id
self.nombre = nombre
def cargaDeDatos(ruta):
with open(ruta) as contenido:
mascotasVendidas = json.load(contenido)
if __name__ == '__main__':
ruta = 'data/archivo.json'
cargaDeDatos(ruta)
What I would like to know is how to save in a python class the data of the people (id and name) that are in the .json file.
I need to create a function that brings me the repeated names
2
Answers
Here’s how you can modify your code to achieve this:
You can use dataclasses and a counter here.
A library you should check out, if you need to work with JSON more often, and especially when generating objects from it is pydantic