Home > database >  Why generating json data from a list (array of arrays) results in a quotation mark problem?
Why generating json data from a list (array of arrays) results in a quotation mark problem?

Time:06-06

Considering the dataframe below:

  timestamp    coordinates
0 [402, 404]   [[2.5719,49.0044], [2.5669,49.0043]]
1 [345, 945]   [[2.5719,49.0044], [2.5669,49.0043]]

I'd like to generate a json file like below:

[
  {
    "vendor": 1,
    "path": [
             [2.5719,49.0044],
             [2.5669,49.0043]
            ],
    "timestamps": [402, 404]
  },
  {
    "vendor": 1,
    "path": [
             [2.5719,49.0044],
             [2.5669,49.0043]
            ],
    "timestamps": [345, 945]
  }]

To do so, my idea is:

  1. For each row of my df, generate a new column geometry containing row json data
  2. Then append all geometries in a json

However, my function below doesn't work.

df["geometry"] = df.apply(lambda row: {
    "vendor": 1,
    "path": row["coordinates"],
    "timestamps": row["timestamp"]
    },
    axis = 1)

Indeed, the result is (for example):
Note the quote marks (') around arrays in path

{
 'vendor': 1,
 'path': ['[2.5719,49.0044]', '[2.5669,49.0043]'],
 'timestamps': [402, 404]
}

Any idea?
Thanks

CodePudding user response:

Presumably the values in coordinates column are of type string. You can use ast.literal_eval to convert it to list:

from ast import literal_eval

df["geometry"] = df.apply(
    lambda row: {
        "vendor": 1,
        "path": literal_eval(row["coordinates"]),
        "timestamps": row["timestamp"],
    },
    axis=1,
)
print(df)

Prints:

    timestamp                           coordinates                                                                                 geometry
0  [402, 404]  [[2.5719,49.0044], [2.5669,49.0043]]  {'vendor': 1, 'path': [[2.5719, 49.0044], [2.5669, 49.0043]], 'timestamps': [402, 404]}
1  [345, 945]  [[2.5719,49.0044], [2.5669,49.0043]]  {'vendor': 1, 'path': [[2.5719, 49.0044], [2.5669, 49.0043]], 'timestamps': [345, 945]}
  • Related