<p>Here is an example of a haskell executable that will convert any of your custom data types to the right JSON format :
<a href="https://github.com/OnChainLabs/Martify/blob/main/app/datum-json.hs" rel="nofollow noreferrer">https://github.com/OnChainLabs/Martify/blob/main/app/datum-json.hs</a></p>
<p>In this file we convert the type <code>NFTSale</code> which is defined like so :</p>
<pre><code>data NFTSale = NFTSale
    { nSeller    :: !PubKeyHash
    , nPrice     :: !Integer
    , nCurrency  :: !CurrencySymbol
    , nToken     :: !TokenName
    } deriving (Generic, ToJSON, FromJSON)
</code></pre>
<p>But it works for any other data type.</p>

CC BY-SA 4.0
2021-11-24T14:14:57.710Z
2021-11-24T14:14:57.710Z
Alain Magazin
<p>I managed to make a working example with more than one parameter. I still don't understand underlying principles, but at least here is something to start working with:</p>
<pre><code>data CustomDatum = CustomDatum
    { num1 :: Integer
    , num2 :: Integer
    }
    deriving Show

PlutusTx.makeIsDataIndexed ''CustomDatum [('CustomDatum, 0)]

-- mkValidator :: Datum -&gt; Redeemer -&gt; ScriptContext -&gt; ()
{-# INLINABLE mkValidator #-}
mkValidator :: CustomDatum -&gt; () -&gt; ScriptContext -&gt; Bool
mkValidator dat () ctx = traceIfFalse &quot;Numbers are missing&quot; numbersAreCorrect
  where
    numbersAreCorrect :: Bool
    numbersAreCorrect = ((num1 dat) == 24) &amp;&amp; ((num2 dat) == 42)
</code></pre>
<p>Example datum</p>
<pre><code>{
    &quot;constructor&quot;:0,
    &quot;fields&quot;:
    [
        {
            &quot;int&quot;:24
        },
        {
            &quot;int&quot;:42
        }
    ]
}
</code></pre>
<p>Example redeemer</p>
<pre><code>{ &quot;constructor&quot;: 0, &quot;fields&quot;:[] }
</code></pre>

CC BY-SA 4.0
2021-11-24T13:38:44.443Z
And as for the `PubKeyHash`, `"bytes":"25e1081f483086365f62fa37618ad6c4768864806fc874b56abe59d7"` works
KugisMugis
CC BY-SA 4.0
2021-11-22T19:35:55.160Z
2021-11-22T19:35:55.160Z
KugisMugis
<p>Let's say I have validator, which checks datum against two numbers, hash, time and a string and there are some funds locked at this script address and I would like to unlock these funds using <code>cardano-cli --tx-in-datum-file...</code> by providing datum file. Inside source code I have a custom data type like so:</p>
<pre><code>data CustomDatum = CustomDatum
    { num1 :: Integer
    , num2 :: Integer
    , hash1 :: PubKeyHash
    , time1 :: POSIXTime
    , string1 :: String
    }
    deriving Show

PlutusTx.unstableMakeIsData ''VestingDatum
</code></pre>
<p>What are the rules to form correct datum file?</p>
<p>How should typed and not typed datum file look like to match my <code>CustomDatum</code> inside Plutus script?</p>
<p>How are values mapped between datum file and custom data inside Plutus script?</p>

<plutus><cardano-cli><on-chain><datum>
How does custom Plutus data type map to cardano-cli --tx-in-datum-file content?
# How does custom Plutus data type map to cardano-cli --tx-in-datum-file content?

(Asked by: username_0 on 2021-11-22)

Let's say I have validator, which checks datum against two numbers, hash, time and a string and there are some funds locked at this script address and I would like to unlock these funds using `cardano-cli --tx-in-datum-file...` by providing datum file. Inside source code I have a custom data type like so:

```
data CustomDatum = CustomDatum
    { num1 :: Integer
    , num2 :: Integer
    , hash1 :: PubKeyHash
    , time1 :: POSIXTime
    , string1 :: String
    }
    deriving Show

PlutusTx.unstableMakeIsData ''VestingDatum

```

What are the rules to form correct datum file?

How should typed and not typed datum file look like to match my `CustomDatum` inside Plutus script?

How are values mapped between datum file and custom data inside Plutus script?

**Tags:** plutus, cardano-cli, on-chain, datum


---

## 2 answers

### Answer by username_1 on 2021-11-24. Score: 1 | Accepted answer

Here is an example of a haskell executable that will convert any of your custom data types to the right JSON format :
<https://github.com/OnChainLabs/Martify/blob/main/app/datum-json.hs>

In this file we convert the type `NFTSale` which is defined like so :

```
data NFTSale = NFTSale
    { nSeller    :: !PubKeyHash
    , nPrice     :: !Integer
    , nCurrency  :: !CurrencySymbol
    , nToken     :: !TokenName
    } deriving (Generic, ToJSON, FromJSON)

```

But it works for any other data type.


---

### Answer by username_0 on 2021-11-22. Score: 0

I managed to make a working example with more than one parameter. I still don't understand underlying principles, but at least here is something to start working with:

```
data CustomDatum = CustomDatum
    { num1 :: Integer
    , num2 :: Integer
    }
    deriving Show

PlutusTx.makeIsDataIndexed ''CustomDatum [('CustomDatum, 0)]

-- mkValidator :: Datum -> Redeemer -> ScriptContext -> ()
{-# INLINABLE mkValidator #-}
mkValidator :: CustomDatum -> () -> ScriptContext -> Bool
mkValidator dat () ctx = traceIfFalse "Numbers are missing" numbersAreCorrect
  where
    numbersAreCorrect :: Bool
    numbersAreCorrect = ((num1 dat) == 24) && ((num2 dat) == 42)

```

Example datum

```
{
    "constructor":0,
    "fields":
    [
        {
            "int":24
        },
        {
            "int":42
        }
    ]
}

```

Example redeemer

```
{ "constructor": 0, "fields":[] }

```

> **Comments:**
> **username_0**: And as for the `PubKeyHash`, `"bytes":"25e1081f483086365f62fa37618ad6c4768864806fc874b56abe59d7"` works  

---

