# JSON decode of Unicode escape sequence: cannot handle more than 4 hex digits?

**URL:** <https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138>\
**Category:** Using Swift\
**Created:** [March 22, 2022, 4:32pm UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138 "2022-03-22T16:32:58Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 22, 2022, 4:32pm UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/1 "2022-03-22T16:32:58Z")

</div>

I want to use Unicode escape sequence for emoji character in my JSON text. But it appears JSONDecoder cannot handle escape sequence of more than 4 hex digits. Of all the examples I see online they are all limited to 4 hex digits or less. But emoji can have scalar value that's five hex digits.

Is the JSONDecoder bad or is JSON unicode code escape just not allow more than 4 hex digits?

```swift
let anEmoji: Character = "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}"
print(anEmoji) // prints: 👨🏻‍❤️‍👨🏻

struct Emoji: Decodable {
    let character: String
}

let json = """
    { "character": "\\u1F468\\u1F3FB\\u200D\\u2764\\uFE0F\\u200D\\u1F468\\u1F3FB"}
    """

let decoder = JSONDecoder()
if let data = json.data(using: .utf8), let result = try? decoder.decode(Emoji.self, from: data) {
    print(result) // expect: 👨🏻‍❤️‍👨🏻, actual: Emoji(character: "὆8ἿB‍❤️‍὆8ἿB")
} else {
    print("No dice!")
}

```

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [March 22, 2022, 5:13pm UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/2 "2022-03-22T17:13:45Z")

</div>

> [@young](#):
>
> Is the JSONDecoder bad or is JSON unicode code escape just not allow more than 4 hex digits?

According to [json.org](http://json.org) at least, it has to be exactly 4 hex chars.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 22, 2022, 5:41pm UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/3 "2022-03-22T17:41:47Z")

</div>

Since some emoji characters have 5 hex digits scalars, so JSON unicode escape sequence cannot express those emoji ☹

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [March 22, 2022, 5:58pm UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/4 "2022-03-22T17:58:24Z")

</div>

Hm, looks like splitting the character into utf-16 codepoints and escaping those works, but I'm not sure whether that should work everywhere or it's just Foundation:

```swift
import Foundation

let anEmoji: Character = "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}"
let str = "\"" + anEmoji.utf16.map { "\\u" + String($0, radix: 16) }.joined() + "\""

let decoder = JSONDecoder()
if let data = str.data(using: .utf8), let result = try? decoder.decode(String.self, from: data) {
    print(result) // works
} else {
    print("No dice!")
}

```

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 22, 2022, 6:41pm UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/5 "2022-03-22T18:41:35Z")

</div>

> [@ahti](#):
>
> Hm, looks like splitting the character into utf-16 codepoints and escaping those works, but I'm not sure whether that should work everywhere or it's just Foundation:

this is part of the [JSON specification](https://spec.json5.org/#escapes), string escape sequences always use UTF16

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 22, 2022, 7:05pm UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/6 "2022-03-22T19:05:29Z")

</div>

Aha, json unicode escape sequence is "`\u`" follow by zero padded 4 hex digits in utf16 encoding

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [March 22, 2022, 11:58pm UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/7 "2022-03-22T23:58:10Z")

</div>

Can't you use this?

```
let json = """
    { "character": "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}"}
"""

```

or even this:

```
let json = "{ \"character\": \"👨🏻‍❤️‍👨🏻\" }"

```

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 23, 2022, 2:51am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/8 "2022-03-23T02:51:38Z")

</div>

It’s not in Swift. The json text is saved to a text file and to be used anywhere as json.

The reason I want to use json Unicode escape sequence instead of actual emoji characters in the file is so the text is plain ascii.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [March 23, 2022, 3:16am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/9 "2022-03-23T03:16:02Z")

</div>

Plain ascii, I see. Then if there's other user content in that JSON, like "René François", or, perhaps "“quotes”", you'll have to convert those to ASCII accordingly: "Ren\u00e9 Fran\u00e7ois", "\u201Cquotes\u201D"

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 23, 2022, 3:19am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/10 "2022-03-23T03:19:56Z")

</div>

It’s only emoji’s and meta data of each emoji parsed from Unicode website.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [March 23, 2022, 3:29am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/11 "2022-03-23T03:29:02Z")

</div>

e.g. "flag: Côte d’Ivoire" 🙂

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 23, 2022, 3:36am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/12 "2022-03-23T03:36:20Z")

</div>

Both Xcode and Vim were not able to display the json file with all those emoji characters all over. It’s totally grey. So I went with escape sequence.

A few characters outside ascii maybe ok? I just want to see what I generated while I’m working things out.

VSCode is fine.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [March 23, 2022, 3:46am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/13 "2022-03-23T03:46:16Z")

</div>

If it works for you - sure, go for it.

BTW, the mentioned 👨🏻‍❤️‍👨🏻 is shown correctly in Xcode for me (although not in an old BBEdit version).

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 23, 2022, 3:52am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/14 "2022-03-23T03:52:20Z")

</div>

Xcode is fine displaying emojis or any Unicode characters. Except it’s not able to display one giant string of emoji json text. I think the very long line is the problem. Same with Vim.

I link my json file in Xcode, click on it and it just a shade of grey. Then I have to use VSCode.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [March 23, 2022, 4:02am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/15 "2022-03-23T04:02:06Z")

</div>

Strange. json files [from here](https://github.com/muan/unicode-emoji-json) open fine for me in Xcode. Maybe your case is more complicated for Xcode to handle.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 23, 2022, 4:07am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/16 "2022-03-23T04:07:05Z")

</div>

I look at those json, they are broken up line by line. Mine has no line break. It’s just one huge line.

I just pretty format it so it’s not one giant line.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [March 23, 2022, 4:15am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/17 "2022-03-23T04:15:19Z")

</div>

I see what you are talking about, looks like Xcode bug. I removed line breaks - and indeed I can see some symbols rendered incorrectly - then I insert a single line break before the sequence of incorrect symbols - and they start rendering correctly.

Perhaps you can split your giant line into a fragments of some reasonable length.

 ![Screenshot 2022-03-23 at 4.12.00Am](https://global.discourse-cdn.com/swift/original/3X/e/f/ef49efb5d9d1ae437241cde794a04cc2d8c8afc8.jpeg)

 ![Screenshot 2022-03-23 at 4.12.08Am](https://global.discourse-cdn.com/swift/original/3X/e/a/eaa664c710990c31d55e37cae8d6a3da4ae92634.jpeg)

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 23, 2022, 4:45am UTC](https://forums.swift.org/t/json-decode-of-unicode-escape-sequence-cannot-handle-more-than-4-hex-digits/56138/18 "2022-03-23T04:45:25Z")

</div>

Hey, surprise! iPad Playground open my json file just fine, I cannot edit the file, maybe it’s because it’s a resource. But text selection, scroll is smooth and fast.

Maybe it’s built with new infrastructure. I hope iPad playground will grow into the new Xcode with no old baggage.
