1

I am trying to parse json files that contain sequences of slashes and backslashes in some of their strings like this:

echo '{"tag_string":"/\/\/\ test"}' | jq

which gives me:

parse error: Invalid escape at line 1, column 27

I have tried escaping with backslashes at different positions, but I can't seem to find a correct way. How do I output the string as it is, without removing any character or getting errors?

This only works on bash, but not sh (or zsh):

echo '{"tag_string":"/\\/\\/\\ test"}' | jq -r '.tag_string'
/\/\/\ test
8
  • So have you tried to look at what echo returns?
    – zerkms
    Mar 2, 2017 at 2:39
  • @zerkms no problems with echo: {"tag_string":"/\/\/\ test"}
    – ae701
    Mar 2, 2017 at 2:42
  • What does "no problems" mean? My point was for you to get what it outputs and see that it's not valid JSON. Take whatever it returns and put into any JSON validator for details.
    – zerkms
    Mar 2, 2017 at 2:46
  • @zerkms I meant by "no problem", this is what I want the final output to be, I already know it's not valid JSON. jsonlint gives me: Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined', and I'm asking how those characters must be escaped to not have an error.
    – ae701
    Mar 2, 2017 at 2:51
  • @ae701 - Your problem description is very confusing because on the one hand you indicate you have some "JSON files", but on the other hand, your examples are tangled up with shell issues. Do you have JSON files, or are you trying to convert some non-JSON files to JSON? ...
    – peak
    Mar 2, 2017 at 4:09

3 Answers 3

2

A forward slash character is legal, but a single backslash character is not. According to json.org char description, the valid chars are:

char
    any-Unicode-character-
        except-"-or-\-or-
        control-character
    \"
    \\
    \/
    \b
    \f
    \n
    \r
    \t
    \u four-hex-digits 

So in your example, the single backslashes are not legal, you need either "\\" which is interpreted as double backslashes, or you need to remove them entirely.

0

If you are trying to include literal backslashes:

(bash)

echo '{"tag_string":"/\\/\\/\\ test"}' | jq
{
  "tag_string": "/\\/\\/\\ test"
}

echo '{"tag_string":"/\\/\\/\\ test"}' | jq -r '.["tag_string"]'
/\/\/\ test

(sh)

echo '{"tag_string":"/\\\\/\\\\/\\\\ test"}' | jq -r '.["tag_string"]'
/\/\/\ test

printf "%s" '{"tag_string":"/\\/\\/\\ test"}' | jq -r '.["tag_string"]'
/\/\/\ test
2
  • This fails with the same message: parse error: Invalid escape at line 1, column 27, and Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined' from npm's jsonlint
    – ae701
    Mar 2, 2017 at 3:02
  • Edit: this actually works on bash, but not sh, which is the shell I'm using for my scripts.
    – ae701
    Mar 2, 2017 at 3:15
0

If you are trying to convert a file with non-JSON strings, then consider a tool such as any-json. Using the "cson-to-json" mode, "\/" will be interpreted as "/":

$ any-json -format=cson

Input:

{"tag_string":"/\/\/\ test"}

Output:

{
  "tag_string": "/// test"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.