-3

My JSON data looks like this:

   [{
        "coverageResponse": "100000\/500000\/1000000",
        "insuranceLine": "COMMERCIAL",
        "coverageCategory": "COVERAGE",
        "carriers": [{
            "carrierId": "LMICO",
            "states": "GA, CA",
            "excludedStates": "PA, NY"
        }, {
            "carrierId": "CNICO",
            "states": "NY, PA",
            "excludedStates": "CA, MI, OH"
        }]
    },
    {
        "coverageResponse": "222\/333\/111",
        "insuranceLine": "COMMERCIAL",
        "coverageCategory": "COVERAGE",
        "carriers": [{
            "carrierId": "LMICO",
            "states": "GA, CA",
            "excludedStates": "PA, NY"
        }, {
            "carrierId": "CNICO",
            "states": "NY, PA",
            "excludedStates": "CA, MI, OH"
        }]
    }]

I want states and excludedStates inside carriers to be in a list

Here is my output expectation:

 [{
        "coverageResponse": "100000\/500000\/1000000",
        "insuranceLine": "COMMERCIAL",
        "coverageCategory": "COVERAGE",
        "carriers": [{
            "carrierId": "LMICO",
            "states": ["GA, CA"],
            "excludedStates": ["PA, NY"]
        }, {
            "carrierId": "CNICO",
            "states": ["NY, PA"],
            "excludedStates": ["CA, MI, OH"]
        }]
    },
   {
    "coverageResponse": "222\/333\/111",
    "insuranceLine": "COMMERCIAL",
    "coverageCategory": "COVERAGE",
    "carriers": [{
        "carrierId": "LMICO",
        "states": "GA, CA",
        "excludedStates": "PA, NY"
    }, {
        "carrierId": "CNICO",
        "states": "NY, PA",
        "excludedStates": "CA, MI, OH"
    }]
}]
  • 1
    What have you tried, and what exactly is the problem with it? – jonrsharpe Aug 27 '20 at 07:36
  • I extracted the json data from a dataframe using this: `jsondata = (mydata.groupby(['coverageResponse', 'insuranceLine', 'coverageCategory'], as_index = True).apply(lambda x: x[['carrierId', 'states', 'excludedStates']].to_dict('r')).reset_index().rename(columns = {0: 'carriers'}).to_json(orient = 'records'))` The problem is, the comma separated values need to be in a list. –  Aug 27 '20 at 07:38
  • [Edit] the question to give a [mre]. – jonrsharpe Aug 27 '20 at 07:39
  • @trincot What is the problem with it? Looks fine to me. –  Aug 27 '20 at 07:40
  • 1
    So your question boils down to ["how to convert a comma-separated string into a list"](https://stackoverflow.com/questions/7844118/how-to-convert-comma-delimited-string-to-list-in-python)? – trincot Aug 27 '20 at 07:41
  • I can use split() for that. But I am not sure how to produce the JSON output where the fields in question are in a list instead of a comma separated string. –  Aug 27 '20 at 07:42
  • Well, that is what `split` does, no? It produces a list instead of a comma separated string. Combine this with ["loop through all nested dictionary values"](https://stackoverflow.com/questions/10756427/loop-through-all-nested-dictionary-values)? ... and I suppose you know about JSON encoding and decoding with the [json API](https://docs.python.org/3/library/json.html)? – trincot Aug 27 '20 at 07:43

1 Answers1

0

Solution for your problem:

init_json = #your json
for i, external in enumerate(init_json):
   for j, internal in enumerate(external['carriers']): 
      init_json[i]['carriers'][j]['states'] = internal['states'].split(', ')
      init_json[i]['carriers'][j]['excludedStates'] = internal['excludedStates'].split(', ')

RoyalGoose
  • 453
  • 9
  • 24