Jump to content
Che

Python: Adaugare new key in Json ?

Recommended Posts

//db_unu.json:
{ 
     "persoane": [{
                 "nume":"Mihai",
				 "prenume":"George",
				 "tipMasina": "Opel"
             },{
                 "nume":"Grigore",
				 "prenume":"Xulescu",
				 "tipMasina": "BMW"
             },{
                 "nume":"Vasile",
				 "prenume":"Marinescu",
				 "tipMasina": "Dacia"
             }]
}
///////////////////////////////////////////////////////////////////////////////
//db_doi.json:
{ 
     "persoane": [{
                 "nume":"Grigore",
				 "prenume":"Xulescu",
				 "oras": "Sibiu"
             },{
                 "nume":"Mihai",
				 "prenume":"George",
				 "oras": "Bucuresti"
             },{
                 "nume":"Vasile",
				 "prenume":"Marinescu",
				 "oras": "Teleorman"
             }]
}

Am cele doua fisiere de tip baza de date in format json.

Cum le unesc in python ca sa imi rezulte un al treilea fisier de tip baza de date, tot json care sa contina pentru fiecare persoana in parte atat tipul de masina cat si orasul fiecareia in parte tinand cont ca: 1) persoanele nu sunt in ordine in cele doua fisiere 2) dar mai pot fi si persoane care se gasesc in unul din fisiere si nu se gasesc in celalalt si vice-versa, caz in care sa-mi adauge key-ul lipsa cu valoarea "NULL" ?

Multumesc mult de tot !

Edited by Che
Link to comment
Share on other sites

Python suporta json nativ.

Gasesti un documentatie cum sa decodezi orice json si sa obtii datele (dupa chei, sortate, etc.)

 

Dupa e simplu sa obtii persone care au aceeasi  nume, acelasi prenume si sa concatenezi masina la jsonul oras.

 

Tutorial:

https://realpython.com/python-json/

daca nu intelegi ceva din tutorial citeste si documentatia:

https://docs.python.org/2/library/json.html

 

LE: Mura-n gura:

The JSON module can also take a JSON string and convert it back to a dictionary structure:

datastore = json.loads(json_string)

https://www.w3schools.com/python/python_dictionaries.asp

https://docs.python.org/2/tutorial/datastructures.html#dictionaries

Edited by yoyois
  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...