Postgres 9.4 JSON: Handle with double quotes inside value - json

I have a json data with double quotes inside value.
For example, '{"resource": "xml version=\"1.0\" "}',
and when I run any json_function I get an error:
select '{"resource": "xml version=\"1.0\" "}'::jsonb - 'resource';
ERROR: invalid input syntax for type json Detail: Expected "," or "}", but found "1.0"
Can somebody explain me, please, how to handle these double quotes at this situation?

Related

Invalid string: control characters in github action - jq

Good Afternoon all,
I am presenting the following problem.
new_ecdsa_config.json looks something like:
{
"certificate": "value long string
multine"
}
When I run the github action for reading the value
- name: Add new ECDSA to Organization
run: |
cat new_ecdsa_config.json | jq '.'
I get the following error:
parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 26, column 53
Error: Process completed with exit code 4.
Any ideas??
When you described the problem on github, you gave this as the example:
{
"certificate": "value long string
multine"
}
As the error message says, that is not valid JSON. (You can double-check this at jsonlint.com if you like.)
If you want the JSON representation of a multiline string, you'd have to escape the newline, e.g. along the lines of:
{
"certificate": "value long string\nmultiline"
}

Ruby Read file without escaping the characters

File Content:
file.txt:
{ 'a' : 'b"c\g' }
Need to parse this JSON.
read_file = File.read(file.txt)
This read_file string is of the form : "{ 'a' : 'b\"c\\g'}\n".
While parsing the JSON:
JSON::ParserError: 757: unexpected token at '{ 'a' : 'b"c\g'}
from /usr/share/ruby/json/common.rb:155:in `parse'
from /usr/share/ruby/json/common.rb:155:in `parse'
from (irb):21
from /usr/bin/irb:12:in `<main>`
The file could contain any escaping sequence or wild-cards, but it will always be in JSON format.
How to parse such JSON file to ruby Hash?
This is because the json in your text file is invalid. Here is a similar question about parsing a json string using single quotes. For this to be valid json it needs
Double quotes
Escaped special characters
Without this you'll be unable to parse it as it won't be recognized as valid json. Try parsing this text instead:
{ "a": "b\"c\\g" } => {"a"=>"b\"c\g"}

Why doesn't Elasticsearch Ingest accept a grok pattern that Logstash does?

I have the following grok pattern that works in Logstash and in the Grok debugger in Kibana.
\[%{TIMESTAMP_ISO8601:req_time}\] %{IP:client_ip} (?:%{IP:forwarded_for}|\(-\)) (?:%{QS:request}|-) %{NUMBER:response_code:int} %{WORD}:%{NUMBER:request_length:int} %{WORD}:%{NUMBER:body_bytes_sent:int} %{WORD}:(?:%{QS:http_referer}|-) %{WORD}:(?:%{QS:http_user_agent}|-) (%{WORD}:(\")?(%{NUMBER:request_time:float})(\")?)?"
I am trying to create a new ingest pipeline via the PUT method, but I get an error that contains:
"type": "parse_exception",
"reason": "Failed to parse content to map",
"caused_by": {
"type": "i_o_exception",
"reason": "Unrecognized character escape '[' (code 91)\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper#61326735; line: 7, column: 25]"
}
Elasticsearch requires that grok patterns used in pipelines submitted using the PUT method are properly escaped JSON, while Logstash patterns use different escaping.
That includes preceding brackets with double backslashes (\\[) and double quotes with triple backslashes (\\\"). The working pattern (after running through a JSON escaping tool) is:
\\[%{TIMESTAMP_ISO8601:req_time}\\] %{IP:client_ip} (?:%{IP:forwarded_for}|\\(-\\)) (?:%{QS:request}|-) %{NUMBER:response_code:int} %{WORD}:%{NUMBER:request_length:int} %{WORD}:%{NUMBER:body_bytes_sent:int} %{WORD}:(?:%{QS:http_referer}|-) %{WORD}:(?:%{QS:http_user_agent}|-) (%{WORD}:(\\\")?(%{NUMBER:request_time:float})(\\\")?)?

Insert json with strings with quotes within them to Postgres

I have some JSON I'm trying to insert into a Postgres database but I can't manage to properly escape the quotes, here's my code
insert into Product_Templates (product) values( '{
"template_id": "OSBSheet",
"name":'Exterior Wall Using 2\"x4\"x96\" Studs, Double Top Plate'
}
I get the error:
invalid command \"x96
How do I work around this?
See the JSON syntax. The keys and string values in JSON are enclosed in double quotes. The quotation marks in strings must be preceded by a "\" character:
select
'{
"template_id": "OSBSheet",
"name": "Exterior Wall Using 2\"x4\"x96\" Studs, Double Top Plate"
}'::jsonb
jsonb
-------------------------------------------------------------------------------------------------
{"name": "Exterior Wall Using 2\"x4\"x96\" Studs, Double Top Plate", "template_id": "OSBSheet"}
(1 row)

Issue with payload when passing user defined value to the body data

When trying to pass the user defined value to the body content, I am getting error "message": "Bad JSON escape sequence: \S. ---- \r\nUnexpected character encountered while parsing value".
When passing complete raw payload through body data, I am not getting this error.
With User defined Variable,
"customerBillingAddress":"26 Chestnut St\Suite 2B\Andover, MA 01810",
convert as " "customerBillingAddress":"26 Chestnut St\Suite 2B\Andover, MA 01810","
"\" is throwing error.
When testing with raw data, I am getting as it is in the payload.
"customerBillingAddress":"26 Chestnut St\\Suite 2B\\Andover, MA 01810",
Please advise
You need to escape \ with \\ and double quote " with \" as per JSON format guideline. Here I think you only need to escape \ in your JSON payload like below .
"customerBillingAddress":"26 Chestnut St\\Suite 2B\\Andover, MA 01810"
You need to escape the following characters in JSON:
\b Backspace (ascii code 08)
\f Form feed (ascii code 0C)
\n New line
\r Carriage return
\t Tab
\" Double quote
\\ Backslash character
In order to do this automatically you can use __groovy() function available since JMeter 3.1 like:
${__groovy(org.apache.commons.lang3.StringEscapeUtils.escapeJson(vars.get('json')),)}
Demo: