# Json encode byte array as hex string instead of base64

**URL:** <https://forum.golangbridge.org/t/json-encode-byte-array-as-hex-string-instead-of-base64/26751>\
**Category:** Getting Help\
**Created:** [March 16, 2022, 10:18am UTC](https://forum.golangbridge.org/t/json-encode-byte-array-as-hex-string-instead-of-base64/26751 "2022-03-16T10:18:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yenle9](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/yenle9/32/11087_2.png) [@Yenle9](https://forum.golangbridge.org/u/Yenle9)\
**Post date:** [March 16, 2022, 10:18am UTC](https://forum.golangbridge.org/t/json-encode-byte-array-as-hex-string-instead-of-base64/26751/1 "2022-03-16T10:18:01Z")

</div>

I’m trying to marshal an interface{} object to json format and print it to a nicer view, it’s almost OK except for byte array.  
I knew that by default

```auto
Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value.

```

But I’d like to print these byte array in hex string instead of base64 (because it’s more farmilliar for a blockchain address/tx).  
I cannot pre-define struct for that and a custom MarshalJson function because this object actually was not initiated by me but decoded from another data format (cbor) then it’s structure may vary.  
Does anyone have workaround for this?  
The data when I print in hex by fmt.Printf("%x\n", data)

```auto
map[0:[[9c37cb5294abce709bfa57bdcab039d75e212a503bc48d5b45e6ff4eb6272759 1] [77df5bd720b6fb8699762fea21bdbc8193a61b770a59443266383ba01a6b8900 1]] 1:[[00177a56a4202e7edd0f394bb850b9dfe072e205f947d7ccb3f8dcb476e10a200512c808028cbbf6758978026a2d8ed307c65de78625d7596c 64] [0067fa035e20501b0507356e7e75278f9f30dad9a8fe3826798b3ee0b0e10a200512c808028cbbf6758978026a2d8ed307c65de78625d7596c a]] 2:64]

```

---

<div class="post-metadata">

**Author:** ![christophberger](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/christophberger/32/11391_2.png) [@christophberger](https://forum.golangbridge.org/u/christophberger)\
**Post date:** [March 17, 2022, 6:51am UTC](https://forum.golangbridge.org/t/json-encode-byte-array-as-hex-string-instead-of-base64/26751/2 "2022-03-17T06:51:39Z")

</div>

Hi @Yenle9,

You can write a custom marshaller, using `encoding/hex` to handle the byte-slice-to-hex conversion. I found an example on StackOverflow: [How to force go to encode `[8]byte` as hex when marshaling to json - Stack Overflow](https://stackoverflow.com/questions/52967943/how-to-force-go-to-encode-8byte-as-hex-when-marshaling-to-json)

---

<div class="post-metadata">

**Author:** ![mje](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/mje/32/10284_2.png) [@mje](https://forum.golangbridge.org/u/mje)\
**Post date:** [March 17, 2022, 2:57pm UTC](https://forum.golangbridge.org/t/json-encode-byte-array-as-hex-string-instead-of-base64/26751/3 "2022-03-17T14:57:04Z")

</div>

Maybe I’m missing something, but that’s not exactly simple for OP because of

> [@Yenle9](#):
>
> marshal an interface{} object…it’s structure may vary

One must use reflection to walk and copy the original object replacing byte arrays with the user-defined type for which the MarshalJSON method is defined, but there’s probably no better way. An even uglier alternative is to post-process the JSON.

---

<div class="post-metadata">

**Author:** ![christophberger](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/christophberger/32/11391_2.png) [@christophberger](https://forum.golangbridge.org/u/christophberger)\
**Post date:** [March 18, 2022, 7:56am UTC](https://forum.golangbridge.org/t/json-encode-byte-array-as-hex-string-instead-of-base64/26751/4 "2022-03-18T07:56:28Z")

</div>

Good point, I clearly overlooked the “may vary” part.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [June 16, 2022, 7:57am UTC](https://forum.golangbridge.org/t/json-encode-byte-array-as-hex-string-instead-of-base64/26751/5 "2022-06-16T07:57:25Z")

</div>

Closed on June 16, 2022, 7:57am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
