Home > Software design >  Turning object column into JSON column
Turning object column into JSON column

Time:05-29

I have an object column in a dataframe with this data structure:

{"sku":"AHG5289"}, {"sku":"MCPV443"}, {"sku":"KBP2646"}, {"sku":"KCB2677"}, {"sku":"OR6344"}, {"sku":"WFM5449"}, {"sku":"TCM3322"}, {"sku":"ADE5357"}, {"sku":"MCP6412"}

And I'm hoping to convert it so that it becomes a proper JSON formatted column with this structure:

[{"sku":"AHG5289"}, {"sku":"MCPV443"}, {"sku":"KBP2646"}, {"sku":"KCB2677"}, {"sku":"OR6344"}, {"sku":"WFM5449"}, {"sku":"TCM3322"}, {"sku":"ADE5357"}, {"sku":"MCP6412"}]

How can I accomplish this?

Edit: I have tried to_json(orient="records") but it adds a bunch of weird backslashes and quotations marks such that it looks like this:

["{\"sku\":\"AHG5289\"}, ..."]

CodePudding user response:

I think you can add [ and ] to the start and end of each row in the column, and use json.loads for each row:

import json
df['col'] = ('['   df['col']   ']').apply(json.loads)
  • Related