Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Include Double Quote in JSON String  RSS feed

 
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to use a remote data result that has a string containing a double quote
but I am getting a syntax error when viewing in a jQgrid. I would like to display with quotes.

My remote String(fieldValue):
Name: SEAT ASSY  TYPE: "A" "SF"

My JSON String:

 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:What are you currently getting?



parsererror: SyntaxError: Invalid character 200 OK
[{"id": 1541803357665, "npaindx": 4424734, "filename": "Divan1", "fieldname": "partno", "fieldvalue": "41325001-504-S185-NW1"},{"id": 1541803423253, "npaindx": 4424734, "filename": "Divan1", "fieldname": "mfgdate", "fieldvalue": "JAN 2018"},{"id": 1541803442126, "npaindx": 4424734, "filename": "Divan1", "fieldname": "serialno", "fieldvalue": "002960340"},{"id": 1541803493907, "npaindx": 4424734, "filename": "Divan1", "fieldname": "bodyline2", "fieldvalue": "NAME: DIVAN 2 PAX TYPE: "A" "SF""},{"id": 1541804882678, "npaindx": 4424734, "filename": "Divan1", "fieldname": "bodyline3", "fieldvalue": "FAA TSO C39c"},{"id": 1541804947026, "npaindx": 4424734, "filename": "Divan1", "fieldname": "bodyline5", "fieldvalue": "LOADS: 9.0 G FWD, 4.42 G UP, 7.36 G DOWN,"},{"id": 1541804978642, "npaindx": 4424734, "filename": "Divan1", "fieldname": "bodyline6", "fieldvalue": "4.0 G SIDE, 1.5 G AFT"},{"id": 1541805009087, "npaindx": 4424734, "filename": "Divan1", "fieldname": "bodyline8", "fieldvalue": "CUSHION ASSEMBLIES COMPLY WITH 14 CFR"},{"id": 1541805036670, "npaindx": 4424734, "filename": "Divan1", "fieldname": "bodyline9", "fieldvalue": "25.853(c) EFFECTIVE MARCH 6, 1995"}]
 
Saloon Keeper
Posts: 2150
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the data that you are being provided is malformed.  The value in line 28 has includes quotation marks which were not properly escaped.

Original:

Corrected:
 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:It looks like the data that you are being provided is malformed.  The value in line 28 has includes quotation marks which were not properly escaped.



Can this be corrected with a replaceAll method?

 
Ron McLeod
Saloon Keeper
Posts: 2150
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Dyke wrote:Can this be corrected with a replaceAll method?


Maybe - but it should be fixed whatever is providing the information, not on the consumer-side.  Shouldn't the information actually be something like: NAME: DIVAN 2 PAX TYPE: ASF ?
 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Steve Dyke wrote:Can this be corrected with a replaceAll method?


Maybe - but it should be fixed whatever is providing the information, not on the consumer-side.  Shouldn't the information actually be something like: NAME: DIVAN 2 PAX TYPE: ASF ?



No this is an FAA Tag and must have the verbiage TYPE: "A" "SF"

The remote data is populated from a java web app interface. So when I write the data I need to set it up that the " get replaced by \"
This is what I expected to work but only " is written. Please help with syntax.

 
Rancher
Posts: 3750
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What service is this coming from?
As Ron says, it's the provider that needs fixing if (and it is an if) this is how it is being sent out.

How are you getting the String?
 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:What service is this coming from?
As Ron says, it's the provider that needs fixing if (and it is an if) this is how it is being sent out.

How are you getting the String?



I have a java web app with an interface to a remote database. Call/Send data using SQL. In this interface I type in a string of data,
click submit and this string is sent to the data base field. This is done for several fields. Then I click a print icon
and the tag prints out of a Zebra printer. So the string I type in to send to the database field is like such:
TYPE: "A" "SF" which works as expected. However, when I use this data to populate a jQgrid I get the syntax error unless
I go into the database manually and replace the " with \" as suggested here.

Now there should be a way to initially send the string to the database with the proper syntax:

So the user types in this string TYPE: "A" "SF" and before the string is sent to the database is converts it to
TYPE: \"A\" \"SF\"

I have tried to use this code but it does not work as expected

 
Dave Tolls
Rancher
Posts: 3750
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is creating the JSON?
What is populating that fieldValue field?
 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:So what is creating the JSON?
What is populating that fieldValue field?



A process within the app. It runs an SQL connection an creates the JSON.

Here is the compiled string when the database filed is like \"A\"

But on the printed placard I have like this "A""(an extra ")

 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:So what is creating the JSON?
What is populating that fieldValue field?



Here is my connection code that save the string to the database:

 
Dave Tolls
Rancher
Posts: 3750
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Dyke wrote:
A process within the app. It runs an SQL connection an creates the JSON.

Here is the compiled string when the database filed is like \"A\"

But on the printed placard I have like this "A""(an extra ")



But what is that process taking in and what is it producing?

Because it looks like it is not producing correct JSON.
 
Dave Tolls
Rancher
Posts: 3750
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This:
"NAME: DIVAN 2 PAX TYPE: ""A"""
contains a couple of escaped double quotes.
So the actual String is:
NAME: DIVAN 2 PAX TYPE: "A"

Looks like the translation process is failing to handle those escapes.
 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:This:
"NAME: DIVAN 2 PAX TYPE: ""A"""
contains a couple of escaped double quotes.
So the actual String is:
NAME: DIVAN 2 PAX TYPE: "A"

Looks like the translation process is failing to handle those escapes.



This is the string from the database I need to translate:
NAME: DIVAN 2 PAX TYPE: \"A\" \"SF\"

This is the translation code:


This is what is printed on the tag:
NAME: DIVAN 2 PAX TYPE: "A" "SF""
 
Dave Tolls
Rancher
Posts: 3750
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I suggest logging the fieldValue before you make any edits to it, and then after.
Not what appears in the tag.
Just what values htis bit of code is dealing with, so straight from your logs.

The result you have just posted is clearly incorrect as you have two " marks at the end there.

Assuming the database has the literal String (this is not escaped in any way):
NAME: DIVAN 2 PAX TYPE: \"A\" \"SF\"
Then I don't see why you need to change anything at all.
 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Can I suggest logging the fieldValue before you make any edits to it, and then after.
Not what appears in the tag.
Just what values htis bit of code is dealing with, so straight from your logs.



Thank you for taking time to help me with this.



Console out:
[11/13/18 7:54:47:334 CST] 0000009e SystemOut     O AbstractLabelCompiler: pre_note2(from data source): NAME: DIVAN 2 PAX TYPE: \"A\" \"SF\"
[11/13/18 7:54:47:334 CST] 0000009e SystemOut     O AbstractLabelCompiler: note2(first translation): "NAME: DIVAN 2 PAX TYPE: \"A\" \"SF\""
[11/13/18 7:54:47:334 CST] 0000009e SystemOut     O AbstractLabelCompiler: note2(second translation): "NAME: DIVAN 2 PAX TYPE: ""A"" ""SF"""
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Dyke wrote:This is the string from the database I need to translate:
NAME: DIVAN 2 PAX TYPE: \"A\" \"SF\"

This is what is printed on the tag:
NAME: DIVAN 2 PAX TYPE: "A" "SF""

This seem like it's getting overly complicated. First off I'd isolate your string manipulation to its own method. Any reason this wouldn't work?
Output:
DIVAN 2 PAX TYPE: \"A\" \"SF\"
DIVAN 2 PAX TYPE: "A" "SF"
 
Dave Tolls
Rancher
Posts: 3750
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now I'm confused.
Why exactly are you trying to turn the escaped quotes in this String into two lots of double quotes?

That will not fix the broken Json.

Indeed, it is the cause of the broken Json.
 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:OK, now I'm confused.
Why exactly are you trying to turn the escaped quotes in this String into two lots of double quotes?

That will not fix the broken Json.

Indeed, it is the cause of the broken Json.



I have two challenges. One is to display the string in a jQgrid using JSON. Two print the string on a label using jQgrid.

So storing the string as NAME: DIVAN 2 PAX TYPE: \"A\" \"SF\" causes it to display in the grid correctly as NAME: DIVAN 2 PAX TYPE: "A" "SF"

However the printing as I showed earlier prints an extra double quote at the end of the string.
 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry should be:
Two print the string on a label using JSON
 
Dave Tolls
Rancher
Posts: 3750
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, if you don't do that conversion you see the string OK in jQGrid?
But it's when you display it in a JLabel (or something) that it goes wrong?

Again, this is without the conversion code you showed...what goes wrong when you do not do that?
What do you see that you should not see?
 
Steve Dyke
Ranch Hand
Posts: 1891
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:So, if you don't do that conversion you see the string OK in jQGrid?
But it's when you display it in a JLabel (or something) that it goes wrong?

Again, this is without the conversion code you showed...what goes wrong when you do not do that?
What do you see that you should not see?



If I store the data as Name: SEAT ASSY  TYPE: "A" "SF" and then try to display in jQgrid I get this:



So I am convinced that I need to store as Name: SEAT ASSY  TYPE: \"A\" \"SF\" to at least get the jQgrid display to work.

When JSON is used to send to print routine each string is wrapped in quotes. For what ever reason if the string contains quotes the end wrapper quote is printed
thus I get a label as   Name: SEAT ASSY  TYPE: "A" "SF""
 
Dave Tolls
Rancher
Posts: 3750
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Dyke wrote:
So I am convinced that I need to store as Name: SEAT ASSY  TYPE: \"A\" \"SF\" to at least get the jQgrid display to work.



OK, I'm not actually sure about this bit.

I think the actual root problem is how you are turning data in your database into a JSON String.

I think I asked this before, but can't see an answer in the thread, but how are you creating the JSON from the data in your database?

Forget about conversion code (like the stuff above that is trying to deal with the escape characters).  I would like to know what the code that takes the various data in the DB and outputs a JSON String looks like.
Are you just concatenating stuff together, or are you creating an instance of a class and then using something like Gson to turn it into a JSON String?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!