<p>also interested, how this Map&lt;Data, Data&gt;() converts to Plutus type? is this (BuiltinData, BuiltinData) a correct way?</p>

CC BY-SA 4.0
2023-11-05T12:09:20.183Z
2023-11-05T12:09:20.183Z
serx
<p>Mesh <code>Data</code> is intended to provide syntactic sugar to produce the Cardano data quickly. It is natively integrated with Mesh native transaction building methods such as <a href="https://meshjs.dev/apis/transaction" rel="nofollow noreferrer"><code>Transaction</code></a> and <a href="https://meshjs.dev/apis/txbuilder" rel="nofollow noreferrer"><code>MeshTxBuilder</code></a>.</p>
<p>If you want to construct the JSON representation of the same type, you might try to do it with our newly documented <a href="https://github.com/MeshJS/mesh/blob/main/packages/mesh-common/src/data/json/primitives.ts#L140" rel="nofollow noreferrer">util functions</a>. Mesh Data of type Map representing the <code>assocMap</code> in Cardano data type. Each string is in form of hex, and wrapped like this:</p>
<pre><code>{
  bytes: &quot;&lt;hex string here&gt;&quot;
}
</code></pre>
<p>From what you have provided, you can try this:</p>
<pre class="lang-typescript prettyprint-override"><code>import {
  assocMap,
  stringToHex,
  byteString,
  list
} from '@meshsdk/core'

const datumMD = assocMap([
  [byteString(stringToHex('name')), byteString(stringToHex(assetName))], // I assume your asset name is utf8 string here
  [byteString(stringToHex('question')), byteString(stringToHex(input))],
  [byteString(stringToHex('answer')), list([])],
  [byteString(stringToHex('results')), list([])],
])
  
const datumState = assocMap([
  [byteString(stringToHex('state')), byteString(stringToHex('INIT'))
])

const datumMetadata = conStr0([datumMD, datumState])
</code></pre>

CC BY-SA 4.0
2024-08-04T08:55:17.320Z
2024-08-04T08:55:17.320Z
SIDAN Whatever
<p>I created this datum with Mesh</p>
<pre><code>  const datumMD: Data = new Map&lt;Data, Data&gt;();
  datumMD.set('name', assetName );
  datumMD.set('question', input);
  datumMD.set('answers', []);
  datumMD.set('results', []);
  
  const datumState: Data = new Map&lt;Data, Data&gt;();
  datumState.set('state', 'INIT');

  const datumMetadata: Data = {
    alternative: 0,
    fields: [datumMD, datumState]
  };
</code></pre>
<p>The CBOR of that is:</p>
<pre><code>d8799fa447616e737765727380446e616d654a70726f706f73616c2d33487175657374696f6e53446f657320697420776f726b3f20486d6d6d3f47726573756c747380a145737461746544494e4954ff
</code></pre>
<p>What would the type of that datum be in Plutus (Haskell)?</p>

CC BY-SA 4.0
2023-01-15T10:10:20.957Z
When creating `datumMD` you are using keys of type String but values of different types. In Haskell, Maps, just like lists, are **homogenous**, therefore you cannot have lists, then strings, then ... in the same map's values. I cannot help further, correct this and then we'll see how to translate your datum to a Haskell data type. 
Also, I'm interested in why you are doing it this way (Mesh -> Plutus) instead of the usual (Plutus -> Mesh)?

Thanks in advance for the clarification :)
Alain Magazin
CC BY-SA 4.0
2023-01-15T10:49:33.557Z
It seems to work, the CBOR of this datum is this: d8799fa447616e737765727380446e616d654a70726f706f73616c2d31487175657374696f6e576e696b6f73203234206c6570746120706172616b616c7747726573756c747380a145737461746544494e4954ff
So maybe its considered all Map of Data ?
Kavvadias
CC BY-SA 4.0
2023-01-15T10:53:23.610Z
Could it be that its
data DatumMetadata = DatumMetadata { datumMD       :: ![(BuiltinData, BuiltinData)],
                                                                   datumState    :: ![(BuiltinData, BuiltinData)]}
Kavvadias
CC BY-SA 4.0
2023-01-15T10:56:51.743Z
I did the Mesh -> Plutus because i needed the datum first on the OffChain part, i didnt use it that much onChain.
Kavvadias
<plutus><haskell><types><mesh>
Data from Mesh to Plutus
# Data from Mesh to Plutus

(Asked by: username_0 on 2023-01-12)

I created this datum with Mesh

```
  const datumMD: Data = new Map<Data, Data>();
  datumMD.set('name', assetName );
  datumMD.set('question', input);
  datumMD.set('answers', []);
  datumMD.set('results', []);
  
  const datumState: Data = new Map<Data, Data>();
  datumState.set('state', 'INIT');

  const datumMetadata: Data = {
    alternative: 0,
    fields: [datumMD, datumState]
  };

```

The CBOR of that is:

```
d8799fa447616e737765727380446e616d654a70726f706f73616c2d33487175657374696f6e53446f657320697420776f726b3f20486d6d6d3f47726573756c747380a145737461746544494e4954ff

```

What would the type of that datum be in Plutus (Haskell)?

**Tags:** plutus, haskell, types, mesh

> **Comments:**
> **username_1**: When creating `datumMD` you are using keys of type String but values of different types. In Haskell, Maps, just like lists, are **homogenous**, therefore you cannot have lists, then strings, then ... in the same map's values. I cannot help further, correct this and then we'll see how to translate your datum to a Haskell data type.  Also, I'm interested in why you are doing it this way (Mesh -> Plutus) instead of the usual (Plutus -> Mesh)?  Thanks in advance for the clarification :)  
> **username_0**: It seems to work, the CBOR of this datum is this: d8799fa447616e737765727380446e616d654a70726f706f73616c2d31487175657374696f6e576e696b6f73203234206c6570746120706172616b616c7747726573756c747380a145737461746544494e4954ff So maybe its considered all Map of Data ?  
> **username_0**: Could it be that its data DatumMetadata = DatumMetadata { datumMD       :: ![(BuiltinData, BuiltinData)],                                                                    datumState    :: ![(BuiltinData, BuiltinData)]}  
> **username_0**: I did the Mesh -> Plutus because i needed the datum first on the OffChain part, i didnt use it that much onChain.  

---

## 2 answers

### Answer by username_2 on 2023-11-05. Score: 0

also interested, how this Map<Data, Data>() converts to Plutus type? is this (BuiltinData, BuiltinData) a correct way?


---

### Answer by username_3 on 2024-08-04. Score: 0

Mesh `Data` is intended to provide syntactic sugar to produce the Cardano data quickly. It is natively integrated with Mesh native transaction building methods such as [`Transaction`](https://meshjs.dev/apis/transaction) and [`MeshTxBuilder`](https://meshjs.dev/apis/txbuilder).

If you want to construct the JSON representation of the same type, you might try to do it with our newly documented [util functions](https://github.com/MeshJS/mesh/blob/main/packages/mesh-common/src/data/json/primitives.ts#L140). Mesh Data of type Map representing the `assocMap` in Cardano data type. Each string is in form of hex, and wrapped like this:

```
{
  bytes: "<hex string here>"
}

```

From what you have provided, you can try this:

```typescript
import {
  assocMap,
  stringToHex,
  byteString,
  list
} from '@meshsdk/core'

const datumMD = assocMap([
  [byteString(stringToHex('name')), byteString(stringToHex(assetName))], // I assume your asset name is utf8 string here
  [byteString(stringToHex('question')), byteString(stringToHex(input))],
  [byteString(stringToHex('answer')), list([])],
  [byteString(stringToHex('results')), list([])],
])
  
const datumState = assocMap([
  [byteString(stringToHex('state')), byteString(stringToHex('INIT'))
])

const datumMetadata = conStr0([datumMD, datumState])

```


---

