Doubt Json loop

0

I have a Json movie file and it is quite long for what I really want but that is not the case, the problem of it being so long is that I want to add several more fields to each movie and I am quite new to the time to work on these types of files (In my case I work with Python) and I want to create a loop to add data to all movies, I know it's pretty simple but I can not find a way to do it no matter how hard I try .

The format of the json file is as follows:

[
{
    "title": "The Shawshank Redemption",
    "rank": "1",
    "id": "tt0111161"
},
{
    "title": "The Godfather",
    "rank": "2",
    "id": "tt0068646"
},
{
    "title": "The Godfather: Part II",
    "rank": "3",
    "id": "tt0071562"
},
{
    "title": "Pulp Fiction",
    "rank": "4",
    "id": "tt0110912"
},
{
    "title": "The Good, the Bad and the Ugly",
    "rank": "5",
    "id": "tt0060196"
]

I have tried several formats of the for but there is no way to get it out, to see if someone can help me a little and enlighten me with the idea, because they modify the data but not add new, in this case I want to add below id another field that is called Oscars, I hope someone can help me!

Greetings!

    
asked by ShJod 26.04.2018 в 18:25
source

2 answers

1

you are right with your idea of the cycle for if you have already defined where you are going to bring your new movies, and more because you have an arrangement of objects. To add a new element you just have to use the push () function that the JS arrangements bring

//tus peliculas
var movies = [
{
    "title": "The Shawshank Redemption",
    "rank": "1",
    "id": "tt0111161"
},
{
    "title": "The Godfather",
    "rank": "2",
    "id": "tt0068646"
},
{
    "title": "The Godfather: Part II",
    "rank": "3",
    "id": "tt0071562"
}
]

//tus nuevas peliculas
var newMovies = [
{
    "title": "Pulp Fiction",
    "rank": "4",
    "id": "tt0110912"
},
{
    "title": "The Good, the Bad and the Ugly",
    "rank": "5",
    "id": "tt0060196"
}
]

and with push () of new movies, you can add items to your list

newMovies.forEach(function(value){
   movies.push(value)
});
    
answered by 26.04.2018 в 18:49
0

What do you think of this form?

jsonEntrada = [
{
    "title": "The Shawshank Redemption",
    "rank": "1",
    "id": "tt0111161"
},
{
    "title": "The Godfather",
    "rank": "2",
    "id": "tt0068646"
},
{
    "title": "The Godfather: Part II",
    "rank": "3",
    "id": "tt0071562"
},
{
    "title": "Pulp Fiction",
    "rank": "4",
    "id": "tt0110912"
},
{
    "title": "The Good, the Bad and the Ugly",
    "rank": "5",
    "id": "tt0060196"
}
]

jsonSalida = list()

for pelicula in jsonEntrada:
    pelicula ['rating'] = 5
    jsonSalida.append(pelicula)

print(jsonSalida)

Changing the JSON you have is as simple as treating it as a dictionary. In this example we add a fixed rating to all the json movies.

If we wanted to iterate over the values of each movie, we would only have to obtain the items from each one:

for pelicula in jsonEntrada:
    for k,v in pelicula.items():
        print(k,'->',v)

Finally remember that jsonEntrada is a list of dictionaries .

    
answered by 26.04.2018 в 19:01