-1

The file is from a slack server export file, so the structure varies every time (if people responded to a thread with text or reactions).

I have tried several SO questions, with similar problems. But I guarantee my question is different. This one, This one too,This one as well

Sample JSON file:

        "client_msg_id": "f347abdc-9e2a-4cad-a37d-8daaecc5ad51",
        "type": "message",
        "text": "I came here just to check <@U3QSFG5A4> This is a sample :slightly_smiling_face:",
        "user": "U51N464MN",
        "ts": "1550511445.321100",
        "team": "T1559JB9V",
        "user_team": "T1559JB9V",
        "source_team": "T1559JB9V",
        "user_profile": {
            "avatar_hash": "gcc8ae3d55bb",
            "image_72": "https:\/\/secure.gravatar.com\/avatar\/fcc8ae3d55bb91cb750438657694f8a0.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0026-72.png",
            "first_name": "A",
            "real_name": "a name",
            "display_name": "user",
            "team": "T1559JB9V",
            "name": "name",
            "is_restricted": false,
            "is_ultra_restricted": false
        },
        "thread_ts": "1550511445.321100",
        "reply_count": 3,
        "reply_users_count": 3,
        "latest_reply": "1550515952.338000",
        "reply_users": [
            "U51N464MN",
            "U8DUH4U2V",
            "U3QSFG5A4"
        ],
        "replies": [
            {
                "user": "U51N464MN",
                "ts": "1550511485.321200"
            },
            {
                "user": "U8DUH4U2V",
                "ts": "1550515191.337300"
            },
            {
                "user": "U3QSFG5A4",
                "ts": "1550515952.338000"
            }
        ],
        "subscribed": false,
        "reactions": [
            {
                "name": "trolldance",
                "users": [
                    "U51N464MN",
                    "U4B30MHQE",
                    "U68E6A0JF"
                ],
                "count": 3
            },
            {
                "name": "trollface",
                "users": [
                    "U8DUH4U2V"
                ],
                "count": 1
            }
        ]
    },

The issue is that there are several keys that vary, so the structure changes within the same json file between messages depending on how other users interact to a given message.

  • what do you need the csv file format for, tell me your application so that I may assist with helping. – Dean Van Greunen Jan 18 '20 at 06:33
  • ***"But I guarantee my question is different."***: Thats not enough, explain in detail **why** the other SO answers don't fit you needs. [Edit] your question and show your attempt and **mandatory** show your expected `CSV` output. – stovfl Jan 18 '20 at 09:49
  • @DeanVanGreunen I want to analyze the content of the conversations, then create a database of which questions have been asked and answered. – Jorge Arellano Jan 21 '20 at 06:37

1 Answers1

0

with open("file.json") as file:
    d = json.load(file)

df = pd.io.json.json_normalize(d)

df.columns = df.columns.map(lambda x: x.split(".")[-1])