简体   繁体   中英

re-map json object for messaging

I'm trying to re-map twitter direct messages data into conversation for mongodb.

"events": [
        {
            "type": "message_create",
            "id": "1023372540847312900",
            "created_timestamp": "1532826001737",
            "message_create": {
                "target": {
                    "recipient_id": "605675237"
                },
                "sender_id": "1020464064684806146",
                "source_app_id": "268278",
                "message_data": {
                    "text": "all right mate thank you too",
                    "entities": {
                        "hashtags": [],
                        "symbols": [],
                        "user_mentions": [],
                        "urls": []
                    }
                }
            }
        },
        {
            "type": "message_create",
            "id": "1023372444210524164",
            "created_timestamp": "1532825978697",
            "message_create": {
                "target": {
                    "recipient_id": "1020464064684806146"
                },
                "sender_id": "605675237",
                "message_data": {
                    "text": "Okay thank you mate, this is distinquish",
                    "entities": {
                        "hashtags": [],
                        "symbols": [],
                        "user_mentions": [],
                        "urls": []
                    }
                }
            }
        },
        {
            "type": "message_create",
            "id": "1023372325150965765",
            "created_timestamp": "1532825950311",
            "message_create": {
                "target": {
                    "recipient_id": "1020464064684806146"
                },
                "sender_id": "69565292",
                "message_data": {
                    "text": "Hello strow bree how are you",
                    "entities": {
                        "hashtags": [],
                        "symbols": [],
                        "user_mentions": [],
                        "urls": []
                    }
                }
            }
        },
        {
            "type": "message_create",
            "id": "1023245595790778372",
            "created_timestamp": "1532795735677",
            "message_create": {
                "target": {
                    "recipient_id": "605675237"
                },
                "sender_id": "1020464064684806146",
                "source_app_id": "268278",
                "message_data": {
                    "text": "Once at a social gathering, Gladstone said to Disraeli",
                    "entities": {
                        "hashtags": [],
                        "symbols": [],
                        "user_mentions": [],
                        "urls": []
                    }
                }
            }
        },
        {
            "type": "message_create",
            "id": "1023245133637140484",
            "created_timestamp": "1532795625491",
            "message_create": {
                "target": {
                    "recipient_id": "1020464064684806146"
                },
                "sender_id": "605675237",
                "message_data": {
                    "text": "On Krat's main screen appeared the holo image of a man, and several dolphins. From the man's shape, Krat could tell it was a female, probably their leader.    \"...stupid creatures unworthy of the name `sophonts.'  Foolish, pre-sentient upspring of errant masters.  We slip away from all your armed might, laughing at your clumsiness!  We slip away as we always will, you pathetic creatures. And now that we have a real head start",
                    "entities": {
                        "hashtags": [],
                        "symbols": [],
                        "user_mentions": [],
                        "urls": []
                    }
                }
            }
        }
    ],
    "apps": {
        "268278": {
            "id": "268278",
            "name": "Twitter Web Client",
            "url": "http://twitter.com"
        }
    }
}

I'm trying to change this data into this;

[{
    members: [ '605675237', '1020464064684806146' ],
    messages: [
        { author: '605675237', body: 'On Krats main screen appeared the holo image of a man, and several dolphins. From the mans shape, Krat could tell it was a female, probably their leader ...stupid creatures unworthy of the name sophonts  Foolish, pre-sentient upspring of errant masters.  We slip away from all your armed might, laughing at your clumsiness!  We slip away as we always will, you pathetic creatures. And now that we have a real head start' },
        { author: '1020464064684806146', body: 'Once at a social gathering, Gladstone said to Disraeli' }
        { author: '605675237', body: 'Okay thank you mate, this is distinquish' }
        { author: '1020464064684806146', body: 'all right mate thank you too' },
    ]
},
{
    members: ['69565292', '1020464064684806146'],
    messages: [
        { author: '69565292', body: 'Hello strow bree how are you' }
    ]
}]

user_id1 must be sender_id and user_id2 must be recipient_id but actually, I have to group the twitter DM Objects by sender_id and recipient_id probably.

How can I solve this problem easily any suggestion?

Maybe we can use Lodash or Underscore to solve this remapping easily.

The solution is the group the messages by the user ids, I have created a hashed from the ids to fit it into an object:

const normalized = {};
data.events.forEach(event => {
  // Just of easier access
  const message = event.message_create;

  // Create a temp hash which identify 2 user conversation.
  // The hash is just the 2 ids with a `|` separated them
  const hashedUsers = [message.sender_id, message.target.recipient_id].sort().join('|');

  // The end object you wanted
  const normalizedMessage = {
    author: message.sender_id,
    body: message.message_data.text
  };

  // Check if we already have this 2 users conversations, create a list of their messages
  if (!normalized.hasOwnProperty(hashedUsers)) {
    normalized[hashedUsers] = [normalizedMessage];
  } else {
    normalized[hashedUsers].push(normalizedMessage);
  }
});


// Now we have a normalized object, the keys are the hash we created and the values are their messages
// so we can create the object you wanted:
const output = []
Object.entries(normalized).forEach(([key, value]) => {
  output.push({
    members: key.split('|'),
    messages: value
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM