24
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jqでparse error: Invalid string: control characters from U+0000 through U+001F must be escapedが出るときの対応

Posted at

はじめに

json形式のレスポンスをパースしようと思ってjqを使ったが、

$ sh log.sh
parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 7, column 3

こういうエラーがでた。

環境

  • WSL
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:        16.04
Codename:       xenial
  • jq
$ jq --version
jq-1.5-1-a5b5cbe

原因

どうやら要素における改行が認められなくなったようで、jq 1.5がこのようなエラーを出します。
https://mbahack2.syui.cf/blog/2015/08/20/json-u-plus-0000-through-u-plus-001f/

ということらしい。

対処法

jqでパースする前にtrで制御文字を全部消してみる。

jq.sh
#!/bin/bash

echo '
[{
  "hoge": "huga",
  "foo": "
    baa
    piyo
  "
}]
' | tr -d '[:cntrl:]' | jq .
$ sh jq.sh
[
  {
    "hoge": "huga",
    "foo": "    baa    piyo  "
  }
]

一応これでやりたいことはできるようになった。

24
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?