0

I have a JSON file. The content is as below,

    [
      {
        "CITY": "Waymill",
        "FIRST_NAME": "Albertine",
        "ID": "1",
        "LAST_NAME": "Jan",
        "SSN": "515-72-7353"
      },
      {
        "CITY": "Spellbridge",
        "FIRST_NAME": "Maryetta",
        "ID": "2",
        "LAST_NAME": "Hoyt",
        "SSN": "515-72-7354"
      },
      {
        "CITY": "Stoneland",
        "FIRST_NAME": "Dustin",
        "ID": "3",
        "LAST_NAME": "Divina",
        "SSN": "515-72-7355"
      },
      {
        "CITY": "Fayview",
        "FIRST_NAME": "Jenna",
        "ID": "4",
        "LAST_NAME": "Sofia",
        "SSN": "515-72-7356"
      },
      {
        "CITY": "Crystalborough",
        "FIRST_NAME": "Wilhelmina",
        "ID": "5",
        "LAST_NAME": "Lavera",
        "SSN": "515-72-7357"
      },
      {
        "CITY": "Janbourne",
        "FIRST_NAME": "Aracelis",
        "ID": "6",
        "LAST_NAME": "Melina",
        "SSN": "515-72-7358"
      },
      {
        "CITY": "Shadowwynne",
        "FIRST_NAME": "Mao",
        "ID": "7",
        "LAST_NAME": "Roselia",
        "SSN": "515-72-7359"
      },
      {
        "CITY": "Foxbridge",
        "FIRST_NAME": "Joni",
        "ID": "8",
        "LAST_NAME": "Hermine",
        "SSN": "515-72-7360"
      },
      {
        "CITY": "Greenmill",
        "FIRST_NAME": "Terrie",
        "ID": "9",
        "LAST_NAME": "Ardelia",
        "SSN": "515-72-7361"
      },
      {
        "CITY": "Glassmont",
        "FIRST_NAME": "Mayme",
        "ID": "10",
        "LAST_NAME": "Shellie",
        "SSN": "515-72-7362"
      },
      {
        "CITY": "Greenlyn",
        "FIRST_NAME": "Louella",
        "ID": "11",
        "LAST_NAME": "Tarah",
        "SSN": "515-72-7363"
      },
      {
        "CITY": "Faypond",
        "FIRST_NAME": "Nevada",
        "ID": "12",
        "LAST_NAME": "Cynthia",
        "SSN": "515-72-7364"
      },
      {
        "CITY": "Rosemarsh",
        "FIRST_NAME": "Candida",
        "ID": "13",
        "LAST_NAME": "Florencia",
        "SSN": "515-72-7365"
      },
      {
        "CITY": "Brightbutter",
        "FIRST_NAME": "Percy",
        "ID": "14",
        "LAST_NAME": "Brenton",
        "SSN": "515-72-7366"
      },
      {
        "CITY": "Glassford",
        "FIRST_NAME": "Nadene",
        "ID": "15",
        "LAST_NAME": "Jenny",
        "SSN": "515-72-7367"
      },
      {
        "CITY": "Snowloch",
        "FIRST_NAME": "Dick",
        "ID": "16",
        "LAST_NAME": "Suzan",
        "SSN": "515-72-7368"
      },
      {
        "CITY": "Blackbush",
        "FIRST_NAME": "Latoya",
        "ID": "17",
        "LAST_NAME": "Rossie",
        "SSN": "515-72-7369"
      },
      {
        "CITY": "Byfay",
        "FIRST_NAME": "Shamika",
        "ID": "18",
        "LAST_NAME": "Yee",
        "SSN": "515-72-7370"
      },
      {
        "CITY": "Redlake",
        "FIRST_NAME": "Kira",
        "ID": "19",
        "LAST_NAME": "Jacquiline",
        "SSN": "515-72-7371"
      },
      {
        "CITY": "Clearpond",
        "FIRST_NAME": "Matilde",
        "ID": "20",
        "LAST_NAME": "Hai",
        "SSN": "515-72-7372"
      },
      {
        "CITY": "Iceston",
        "FIRST_NAME": "Drusilla",
        "ID": "21",
        "LAST_NAME": "Rhonda",
        "SSN": "515-72-7373"
      },
      {
        "CITY": "Linkeep",
        "FIRST_NAME": "Stuart",
        "ID": "22",
        "LAST_NAME": "Jeannette",
        "SSN": "515-72-7374"
      },
      {
        "CITY": "Icenesse",
        "FIRST_NAME": "Lorena",
        "ID": "23",
        "LAST_NAME": "Jimmie",
        "SSN": "515-72-7375"
      },
      {
        "CITY": "Northbarrow",
        "FIRST_NAME": "Myra",
        "ID": "24",
        "LAST_NAME": "Rubi",
        "SSN": "515-72-7376"
      },
      {
        "CITY": "Winterfay",
        "FIRST_NAME": "Marylee",
        "ID": "25",
        "LAST_NAME": "Dara",
        "SSN": "515-72-7377"
      },
      {
        "CITY": "Westerden",
        "FIRST_NAME": "Easter",
        "ID": "26",
        "LAST_NAME": "Jin",
        "SSN": "515-72-7378"
      },
      {
        "CITY": "Snowburn",
        "FIRST_NAME": "Bertha",
        "ID": "27",
        "LAST_NAME": "Eura",
        "SSN": "515-72-7379"
      },
      {
        "CITY": "Marblegrass",
        "FIRST_NAME": "Chieko",
        "ID": "28",
        "LAST_NAME": "Darcy",
        "SSN": "515-72-7380"
      },
      {
        "CITY": "Wheatville",
        "FIRST_NAME": "Evelina",
        "ID": "29",
        "LAST_NAME": "Cathie",
        "SSN": "515-72-7381"
      },
      {
        "CITY": "Highfalcon",
        "FIRST_NAME": "Tatiana",
        "ID": "30",
        "LAST_NAME": "Ramon",
        "SSN": "515-72-7382"
      },
      {
        "CITY": "Wellshadow",
        "FIRST_NAME": "Deon",
        "ID": "31",
        "LAST_NAME": "Shyla",
        "SSN": "515-72-7383"
      },
      {
        "CITY": "Faymeadow",
        "FIRST_NAME": "Dewayne",
        "ID": "32",
        "LAST_NAME": "Wilda",
        "SSN": "515-72-7384"
      },
      {
        "CITY": "Hollowmarsh",
        "FIRST_NAME": "Meg",
        "ID": "33",
        "LAST_NAME": "Hillary",
        "SSN": "515-72-7385"
      },
      {
        "CITY": "Greenview",
        "FIRST_NAME": "Tiana",
        "ID": "34",
        "LAST_NAME": "Edmundo",
        "SSN": "515-72-7386"
      },
      {
        "CITY": "Lakeley",
        "FIRST_NAME": "Melodi",
        "ID": "35",
        "LAST_NAME": "Reinaldo",
        "SSN": "515-72-7387"
      },
      {
        "CITY": "Deeracre",
        "FIRST_NAME": "Michaela",
        "ID": "36",
        "LAST_NAME": "Babette",
        "SSN": "515-72-7388"
      },
      {
        "CITY": "Mallowness",
        "FIRST_NAME": "Zack",
        "ID": "37",
        "LAST_NAME": "Aletha",
        "SSN": "515-72-7389"
      },
      {
        "CITY": "Bridgemill",
        "FIRST_NAME": "Keshia",
        "ID": "38",
        "LAST_NAME": "Coretta",
        "SSN": "515-72-7390"
      },
      {
        "CITY": "Shadowwick",
        "FIRST_NAME": "Gillian",
        "ID": "39",
        "LAST_NAME": "Nicki",
        "SSN": "515-72-7391"
      },
      {
        "CITY": "Violetbeach",
        "FIRST_NAME": "Elvie",
        "ID": "40",
        "LAST_NAME": "Fidelia",
        "SSN": "515-72-7392"
      },
      {
        "CITY": "Westerdell",
        "FIRST_NAME": "Sheron",
        "ID": "41",
        "LAST_NAME": "Robin",
        "SSN": "515-72-7393"
      },
      {
        "CITY": "Icebank",
        "FIRST_NAME": "Jovan",
        "ID": "42",
        "LAST_NAME": "Della",
        "SSN": "515-72-7394"
      },
      {
        "CITY": "Vertloch",
        "FIRST_NAME": "Chadwick",
        "ID": "43",
        "LAST_NAME": "Letitia",
        "SSN": "515-72-7395"
      },
      {
        "CITY": "Janmill",
        "FIRST_NAME": "Kellee",
        "ID": "44",
        "LAST_NAME": "Jess",
        "SSN": "515-72-7396"
      },
      {
        "CITY": "Westerhaven",
        "FIRST_NAME": "Patrick",
        "ID": "45",
        "LAST_NAME": "Meghan",
        "SSN": "515-72-7397"
      },
      {
        "CITY": "Eastwald",
        "FIRST_NAME": "Dulcie",
        "ID": "46",
        "LAST_NAME": "Winnie",
        "SSN": "515-72-7398"
      },
      {
        "CITY": "Deeracre",
        "FIRST_NAME": "Meri",
        "ID": "47",
        "LAST_NAME": "Ranee",
        "SSN": "515-72-7399"
      },
      {
        "CITY": "Mallowness",
        "FIRST_NAME": "Jeannette",
        "ID": "48",
        "LAST_NAME": "Lavona",
        "SSN": "515-72-7400"
      },
      {
        "CITY": "Bridgemill",
        "FIRST_NAME": "Claretta",
        "ID": "49",
        "LAST_NAME": "Kallie",
        "SSN": "515-72-7401"
      },
      {
        "CITY": "Shadowwick",
        "FIRST_NAME": "Detra",
        "ID": "50",
        "LAST_NAME": "Donald",
        "SSN": "515-72-7402"
      }
    ]

I am passing a input parameter as,

{
  "column_names": "['ID', 'CITY', 'SSN']"
}

and in the python side, I am getting the input as,

['ID', 'CITY', 'SSN']

now, what I am trying to do is to fetch the actual JSON file and create a new JSON file only for those given key values from the input.

So far, the code is as below in python3x,

import json

column_names = request.get_json()['column_names']

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

getdata = []
for each in output_res:
    d = {'ID': each['ID'],
          'SSN':each['SSN'],
          'CITY':each['CITY']}
    getdata.append(d)

with open('outputfile.json','w', encoding='utf-8') as f:
    json.dump(getdata,f,ensure_ascii=False)

Now, in the above code, I am able to create a new file with fixed key/value pairs, not using the input parameter.

The input parameter variable can have one or more arguments or column names.

Tried so many things, but, unable to implement those successfully.

PS: I am new to python.

Debraj Das
  • 79
  • 3
  • 16

1 Answers1

4

You're nearly there! All you need to do is to get the column names from the column_names variable.

import json

column_names = request.get_json()['column_names']

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

getdata = []
for each in output_res:
    d = {x: each[x] for x in json.loads(column_names)}  # see here
    getdata.append(d)

with open('outputfile.json','w', encoding='utf-8') as f:
    json.dump(getdata,f,ensure_ascii=False)

Note that ...

{x: each[x] for x in column_names}

... is called a dictionary comprehension, and is just syntactic sugar for:

d = {}
for x in column_names:
    d[x] = each[x]
mackorone
  • 1,056
  • 6
  • 15
  • Also as a bonus, [dictionary comprehension is faster](https://stackoverflow.com/q/52542742/2217801) – Jesse Reza Khorasanee Sep 11 '19 at 04:57
  • thanks for the quick guideline. But, I am now getting the below error. Any idea please.. d = {x: each[x] for x in column_names} # see here KeyError: '[' – Debraj Das Sep 11 '19 at 05:05
  • Ah yes, this is tricky. I just realized that you *also* need to deserialize `column_names` from a JSON string into a list, like this: `column_names = json.loads(request.get_json()['column_names'])`. Otherwise, `for x in column_names` will traverse each character in the string `"['ID', 'CITY', 'SSN']"` rather than each element in the list `['ID', 'CITY', 'SSN']`. – mackorone Sep 11 '19 at 05:11
  • understood.. but, what if the current input is as : ['ID', 'CITY', 'SSN'] by updating the code as per the above guideline it's giving me error. – Debraj Das Sep 11 '19 at 05:21
  • @DebrajDas I'm not sure I understand... why specifically is your question? What is the error you're seeing? Note that single quotes aren't valid JSON, so you may need to convert them to double quotes first (and you may have to escape the double-quotes since it's nested JSON). – mackorone Sep 11 '19 at 05:23
  • I am sending parameters from postman as `code`{ "column_names": "['ID', 'CITY', 'SSN']" } in the python side, I am getting the value as, `code`['ID', 'CITY', 'SSN'] from the variable, `code` column_names = request.get_json()['column_names'] – Debraj Das Sep 11 '19 at 05:38
  • and for the below code, `code`getdata = [] for each in output_res: d = {x: each[x] for x in json.loads(column_names)} getdata.append(d) print(getdata) i am getting the below error, raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1) – Debraj Das Sep 11 '19 at 05:39
  • Right. The `JSONDecodeError` is because single quotes aren't valid JSON. You need to update the input to use double quotes, i.e., `["ID", "CITY", "SSN"]` – mackorone Sep 11 '19 at 05:41
  • @mackorone thanks a lot for your quick response and guidelines.. Yes, Now it's working.. I had to convert from all the single quote to double code.. Thanks again – Debraj Das Sep 11 '19 at 05:48