This might be a possible duplicate with this.
I want to know a proper way to insert additional string with OP_RETURN and retrieve the transaction from block chain and decode it accordingly.
Essentially, one of the output scripts should be
OP_RETURN <the data you want to add>
Let me give an example. Say you want to add the string "Melons." to the blockchain.
That's... trickier. Bitcoin is not optimized for searching the metadata of transactions.