<p>Reading some old messages in discord, someone mentioned that I could use <code>encode (scriptDataToJson ScriptDataJsonDetailedSchema $ fromPlutusData data)</code>, taken from <a href="https://github.com/input-output-hk/Alonzo-testnet/blob/main/resources/plutus-sources/plutus-helloworld/app/plutus-helloworld-bytestring.hs#L43" rel="nofollow noreferrer">here</a>, so I made a <code>redeemerJSON</code> function defined as follows:</p>
<pre><code>redeemerJSON :: DistributionRedeemer -&gt; ByteString
redeemerJSON dr =
  encode (scriptDataToJson ScriptDataJsonDetailedSchema $ fromPlutusData (PlutusTx.toData dr))
</code></pre>
<p>Of course some imports are necessary, but I believe you can use this same approach for the datum and other script parameters too.</p>

CC BY-SA 4.0
2021-11-24T01:11:41.747Z
I would make it even more general to: 

```haskell
plutusDataToJSON :: PlutusTx.ToData a => a ->  Data.ByteString.Lazy.Internal.ByteString
plutusDataToJSON = encode . (scriptDataToJson ScriptDataJsonDetailedSchema) . fromPlutusData . Plutus.toData```
yoyobigmanyo
CC BY-SA 4.0
2021-10-25T21:30:48.917Z
2021-10-25T21:30:48.917Z
Mateus
<p>The Cardano-node github is the best resource that I have found</p>
<h2>JSON conversion</h2>
<blockquote>
<p>Script data is similar to JSON but not exactly the same. It has some
deliberate limitations such as no support for floating point numbers
or special forms for null or boolean values. It also has limitations
on the length of strings.</p>
<p>On the other hand, unlike JSON, it
distinguishes between byte strings and text strings. It also supports
any value as map keys rather than just string. It also supports
alternatives / tagged unions, used for  representing constructors for
Plutus data values.</p>
<p>continues ....</p>
</blockquote>
<p>Further extensive details in Cardano-node github file <a href="https://github.com/input-output-hk/cardano-node/blob/c1efb2f97134c0607c982246a36e3da7266ac194/cardano-api/src/Cardano/Api/ScriptData.hs#L254" rel="nofollow noreferrer">ScriptData.hs</a></p>

CC BY-SA 4.0
2022-02-17T15:37:29.460Z
2022-02-18T01:58:48.317Z
2022-02-18T01:58:48.317Z
Walker Leite
<p>By following the alonzo-testnet exercises, I'm right now having to build a transaction with <code>cardano-cli</code>. One of the arguments necessary to consume a script is the datum, another is the redeemer, both of which should be in a JSON file (or value).</p>
<p>Is there any reference of how <code>Data</code> should be expressed in JSON? Because I tried converting it with Data.Aeson in Haskell, but received the following error</p>
<pre><code>JSON object does not match the schema.
Expected a single field named &quot;int&quot;, &quot;bytes&quot;, &quot;string&quot;, &quot;list&quot; or &quot;map&quot;.
Unexpected object field(s): {&quot;contents&quot;:{&quot;getPubKeyHash&quot;:&quot;3e7181860513a17dfa44d69232429e5c328e79c748d4a176950c42b6&quot;},&quot;tag&quot;:&quot;Collect&quot;}
</code></pre>
<p>Also, is there any library to make my life easier, so I can be able to easily convert my redeemers into JSON?</p>

<plutus><transaction>
Convert Redeemer, Datum or Script Parameter to JSON
# Convert Redeemer, Datum or Script Parameter to JSON

(Asked by: username_0 on 2021-10-25)

By following the alonzo-testnet exercises, I'm right now having to build a transaction with cardano-cli. One of the arguments necessary to consume a script is the datum, another is the redeemer, both of which should be in a JSON file (or value).
Is there any reference of how Data should be expressed in JSON? Because I tried converting it with Data.Aeson in Haskell, but received the following error
JSON object does not match the schema.
Expected a single field named "int", "bytes", "string", "list" or "map".
Unexpected object field(s): {"contents":{"getPubKeyHash":"3e7181860513a17dfa44d69232429e5c328e79c748d4a176950c42b6"},"tag":"Collect"}

Also, is there any library to make my life easier, so I can be able to easily convert my redeemers into JSON?

**Tags:** plutus, transaction

---

## 2 answers

### Answer by username_0 on 2021-10-25. Score: 1

Reading some old messages in discord, someone mentioned that I could use encode (scriptDataToJson ScriptDataJsonDetailedSchema $ fromPlutusData data), taken from here, so I made a redeemerJSON function defined as follows:
redeemerJSON :: DistributionRedeemer -> ByteString
redeemerJSON dr =
  encode (scriptDataToJson ScriptDataJsonDetailedSchema $ fromPlutusData (PlutusTx.toData dr))

Of course some imports are necessary, but I believe you can use this same approach for the datum and other script parameters too.

> **Comments:**
> **username_1**: I would make it even more general to:   ```haskell plutusDataToJSON :: PlutusTx.ToData a => a ->  Data.ByteString.Lazy.Internal.ByteString plutusDataToJSON = encode . (scriptDataToJson ScriptDataJsonDetailedSchema) . fromPlutusData . Plutus.toData```  

---

### Answer by username_2 on 2022-02-17. Score: 0

The Cardano-node github is the best resource that I have found

## JSON conversion

> Script data is similar to JSON but not exactly the same. It has some
> deliberate limitations such as no support for floating point numbers
> or special forms for null or boolean values. It also has limitations
> on the length of strings.
>
> On the other hand, unlike JSON, it
> distinguishes between byte strings and text strings. It also supports
> any value as map keys rather than just string. It also supports
> alternatives / tagged unions, used for representing constructors for
> Plutus data values.
>
> continues ....

Further extensive details in Cardano-node github file [ScriptData.hs](https://github.com/input-output-hk/cardano-node/blob/c1efb2f97134c0607c982246a36e3da7266ac194/cardano-api/src/Cardano/Api/ScriptData.hs#L254)


---

