Teett
Teett

Reputation: 43

Remove brackets created by a list through json.dumps()

I'm trying to connect to an API that is requesting the following input format:

{
    "clientId":"some_client",
    "apiKey":"some_key",
    "requestTime":1620912445968,
    "sha":"some_sha",
    "owner":"[email protected]",
    "contacts": [
    {
      "addresseeType": "email",
      "value": "[email protected], [email protected]"
    }
  ]
}

My issue with this is that the emails inside "value" end up with brackets when I use json.dumps() if my input is a list. My take on this is the following:

# I need to take the emails from a pandas df
df = pd.DataFrame({'email': ["[email protected]", "[email protected]"]})

dict_params = {
        "clientId":"some_client",
        "apiKey":"some_key",
        "requestTime": "millis",
        "sha":"some_sha",
        "owner": "[email protected]",
        "contacts": [
            {
              "addresseeType": "email",
              "value": df["email"].to_list()
              }
            ]
        }

result = json.dumps(dict_params)

This gives a valid JSON as expected, but it's not what my API is asking for, as the emails must not have brackets.

The result:

{
  "clientId": "some_client",
  "apiKey": "some_key",
  "requestTime": "millis",
  "sha": "some_sha",
  "owner": "[email protected]",
  "contacts": [
    {
      "addresseeType": "email",
      "value": [
        "[email protected]",
        "[email protected]"
      ]
    }
  ]
}

Any help on this is appreciated!! thank you.

Upvotes: 0

Views: 319

Answers (1)

DeepSpace
DeepSpace

Reputation: 81604

You should use ', '.join:

'value': ', '.join(df['email'])

e.g

df = pd.DataFrame({'email': ['[email protected]', '[email protected]']})
print(', '.join(df['email']))
# [email protected], [email protected]

Upvotes: 3

Related Questions