Home > Software engineering >  How can I print ALL my json nested data in the end of each "father" line in python?
How can I print ALL my json nested data in the end of each "father" line in python?

Time:04-16

import json


with open('json.json') as f:
    data = json.load(f)


for event in data['input']:
    edition = event.get('edition', '')
    name = event['name']
    start_date = event['startDate']
    end_date = event.get('endDate', '')

    for location in event['location']:
        city= location.get('city', '')
        state= location.get('state', '')
        country= location.get('country', '')
    
    
    print(edition,name,start_date,end_date,city,state,country)


---
{
    "input": [
        {
            "edition": "4th",
            "name": "JBCN Conference",
            "startDate": "2018-06-11",
            "endDate": "2018-06-13",
            "location": [
                {
                    "city": "Barcelona",
                    "country": "Spain"
                }
            ]
 },
 {
     "edition": "3rd",
     "name": "DevTernity",
     "startDate": "2018-11-30",
     "endDate": "2018-12-01",
     "location": [
         {
             "city": "Riga",
             "country": "Latvia"
         }
     ]
 },
 {
     "edition": "1st",
     "name": "I T.A.K.E Unconference",
     "startDate": "2016-05-19",
     "endDate": "2016-05-20",
     "location": [
         {
             "city": "Bucharest",
             "country": "Romania"
         },
         {
             "city": "Maramures",
             "country": "Romania"
         }
     ]
 },
 {
     "edition": "2nd",
     "name": "Product Owner Rule Book",
     "startDate": "2016-04-11",
     "endDate": "2016-04-13",
     "location": [
         {
             "city": "Paris",
             "country": "France"
         },
         {
             "city": "Madrid",
             "country": "Spain"
         }
     ]
 },
 {
     "name": "Upfront Summit",
     "startDate": "2018-02-01",
     "location": [
         {
             "city": "Los Angeles",
             "state": "California",
             "country": "United States"
         }
     ]
 },
 {
     "name": "IBM Think",
     "startDate": "2018-03-19",
     "location": [
       {
             "state": "Nevada",
             "country": "United States"
       }
     ]
    }
  ]
}

I only get the last nested item for city,state,country, BUT I want to print all the cities, states and countries of each event in the same line.

Current Output:

  • 4th JBCN Conference 2018-06-11 2018-06-13 Barcelona Spain
  • 3rd DevTernity 2018-11-30 2018-12-01 Riga Latvia
  • 1st I T.A.K.E Unconference 2016-05-19 2016-05-20 Maramures Romania
  • 2nd Product Owner Rule Book 2016-04-11 2016-04-13 Madrid Spain
  • Upfront Summit 2018-02-01 Los Angeles California United States
  • IBM Think 2018-03-19 Nevada United States

Desired Output:

  • 4th JBCN Conference · 2018-06-11 / 2018-06-13 · Barcelona, Spain
  • 3rd DevTernity · 2018-11-30 / 2018-12-01 · Riga, Latvia
  • 1st I T.A.K.E Unconference · 2016-05-19 / 2016-05-20 · Bucharest | Maramures, Romania
  • 2nd Product Owner Rule Book · 2016-04-11 / 2016-04-13 · Paris, France | Madrid, Spain
  • Upfront Summit · 2018-02-01 · Los Angeles, California. United States
  • IBM Think · 2018-03-19 · Nevada, United States

CodePudding user response:

Because in your location there is more than one location. That is why city, state, country gets last one. Look you set city = firstCity and then you set city = secondCity. Here your location for loop sets last value to variables. And you print it outside of location loop. just print it in location loop or do something similar to my code.

for event in data['input']:
    edition = event.get('edition', '')
    name = event['name']
    start_date = event['startDate']
    end_date = event.get('endDate', '')
    loc = ''
    for location in event['location']:
        city = location.get('city', '')
        state = location.get('state', '')
        country = location.get('country', '')
        if city:
            loc  = city
        if state:
            loc  = ' | '   state
        if country:
            loc  = ' | '   country
        if location != event['location'][-1]:
            loc  = ', '

    print(edition, name, start_date, end_date, loc)

and here is output:

4th JBCN Conference 2018-06-11 2018-06-13 Barcelona | Spain
3rd DevTernity 2018-11-30 2018-12-01 Riga | Latvia
1st I T.A.K.E Unconference 2016-05-19 2016-05-20 Bucharest | Romania, Maramures | Romania
2nd Product Owner Rule Book 2016-04-11 2016-04-13 Paris | France, Madrid | Spain
 Upfront Summit 2018-02-01  Los Angeles | California | United States
 IBM Think 2018-03-19   | Nevada | United States

Here if you print inside of location loop:

for event in data['input']:
    edition = event.get('edition', '')
    name = event['name']
    start_date = event['startDate']
    end_date = event.get('endDate', '')
    for location in event['location']:
        city = location.get('city', '')
        state = location.get('state', '')
        country = location.get('country', '')

        print(edition, name, start_date, end_date, city, state, country)

your output would be:

4th JBCN Conference 2018-06-11 2018-06-13 Barcelona  Spain
3rd DevTernity 2018-11-30 2018-12-01 Riga  Latvia
1st I T.A.K.E Unconference 2016-05-19 2016-05-20 Bucharest  Romania
1st I T.A.K.E Unconference 2016-05-19 2016-05-20 Maramures  Romania
2nd Product Owner Rule Book 2016-04-11 2016-04-13 Paris  France
2nd Product Owner Rule Book 2016-04-11 2016-04-13 Madrid  Spain
 Upfront Summit 2018-02-01  Los Angeles California United States
 IBM Think 2018-03-19   Nevada United States

if you don't want to repeat printed locations. Just do something similar to my first example

CodePudding user response:

Does this work for you?

EDIT - BTW, your problem is you are reassigning variables with each iteration of city and country, you need to add them to the variable eg city = " " location.get('city', '')

from json import loads as json_loads
from re import sub as re_sub

data = json_loads(open('json.json').read())


for event in data['input']: 
    output = " · ".join([i for i in event.values() if type(i) != list])
    for location in event['location']:
        output  = " · "   ", ".join([i for i in location.values()])
    output = re_sub(r"(\d) · (\d)", r"\1 / \2", output)
    print(output)

output:

4th · JBCN Conference · 2018-06-11 / 2018-06-13 · Barcelona, Spain
3rd · DevTernity · 2018-11-30 / 2018-12-01 · Riga, Latvia
1st · I T.A.K.E Unconference · 2016-05-19 / 2016-05-20 · Bucharest, Romania · Maramures, Romania
2nd · Product Owner Rule Book · 2016-04-11 / 2016-04-13 · Paris, France · Madrid, Spain
Upfront Summit · 2018-02-01 · Los Angeles, California, United States
IBM Think · 2018-03-19 · Nevada, United States
  • Related