what is wrong with my JSON structure? - json

{"Street":
[
{
"Street_name":"Dewlane Dr",
"Street_numbers":
[
{
"number":26,
"Unit_number":""
}
]
}
]
}
but i get this error while parsing it on iPhone thorough SBJSON.
Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: Street\" UserInfo=0x595fd60 {NSUnderlyingError=0x593cfc0

There's an extra closing quote after "Unit_number". Ah, that was just a copy-and-paste error.
There's nothing wrong with it, as far as I can see and according to jsonlint.com.
Just to break it down: Your structure has one outermost object, which has a Street property. The Street property's value is an array with one entry. That one entry is an object with the properties Street_name, which has a string value, and Street_numbers, which has an array value. The one entry in the Street_numbers array is an object with two properties, number and Unit_number.
Update: I never addressed the SBJSON error.
If you're getting that error from SBJSON, it suggests one of the following:
What you're feeding into SBJSON isn't what you've quoted (it's quite an easy mistake to make). For instance, perhaps what you're feeding in has gotten truncated in some way just after "Street":.
You have a character after "Street": looks like whitespace by the time you've pasted it into StackOverflow, but isn't whitespace by the usual definitions, and so SBJSON thinks it's a character that should be processed and is choking on it.
SBJSON has a bug in it related to the value being on the line following the colon (which seems unlikely).
SBJSON has a bug related to the precise kind of whitespace you have at that location. There are lots of different whitespace charactesrs in Unicode, if you've (probably inadvertently) used an unusual one, SBJSON may not be handling it correctly.
But if what looks like whitespace in what you posted is comprised entirely of carriage returns, spaces, tabs, line feeds, and other classic whitespace, and if the string you're passing to SBJSON's deserializer is what you've posted, it's a problem with SBJSON.

Related

Regex for replacing unnecessary quotation marks within a JSON object containing an array

I am currently trying to format a JSON object using LabVIEW and have ran into the issue where it adds additional quotation marks invalidating my JSON formatting. I have not found a way around this so I thought just formatting the string manually would be enough.
Here is the JSON object that I have:
{
"contentType":"application/json",
"content":{
"msgType":2,
"objects":"["cat","dog","bird"]",
"count":3
}
}
Here is the JSON object I want with the quotation marks removed.
{
"contentType":"application/json",
"content":{
"msgType":2,
"objects":["cat","dog","bird"],
"count":3
}
}
I am still not an expert with regex and using a regex tester I was only able to grab the "objects" and "count" fields but I would still feel I would have to utilize substrings to remove the quotation marks.
Example I am using (would use a "count" to find the start of the next field and work backwards from there)
"([objects]*)"
Additionally, all the other Regex I have been looking at removes all instances of quotation marks whereas I only need a specific area trimmed. Thus, I feel that a specific regex replace would be a much more elegant solution.
If there is a better way to go about this I am happy to hear any suggestions!
Your question suggests that the built-in LabVIEW JSON tools are insufficient for your use case.
The built-in library converts LabVIEW clusters to JSON in a one-shot approach. Bundle all your data into a cluster and then convert it to JSON.
When it comes to parsing JSON, you use the path input terminal and the default type terminals to control what data is parsed from a JSON string.
If you need to handle JSON in a manner similar to say JavaScript, I would recommend something like the JSONText Toolkit which is free to use (and distribute) under the BSD licence. This allows more complex and iterative building of JSON strings from LabVIEW types and has text-path style element access along with many more features.
The Output controls from both my examples are identical - although JSONText provides a handy Pretty Print vi.
After using a regex from one of the comments, I ended up with this regex which allowed me to match the array itself.
(\[(?:"[^"]*"|[^"])+\])
I was able to split the the JSON string into before match, match and after match and removed the quotation marks from the end of 'before match' and start of 'after match' and concatenated the strings again to form a new output.

How to find JSON parse error in existing Ruby code

First let me say that while I am a programmer, Ruby is very new to me. I'm trying to fix an application created by another developer, but I cannot find the issue. When the code is run, I get the error below:
JSON::ParserError: unexpected token at '{
"account":"xxxxxxx",
"role":"3",
"email":"xxxx#xxxxxxxx.com",
"password":'xxxxxxxxxxxx',
"connect_host":"xxxxxxx.xxxxxxx.api.xxxxxxxx.com"
}
'
Account is numeric. Email is standard/no special characters. Password does have special characters, on of them being a double quote which is why it is wrapped in single quotes. Connect host is just an URL with alpha-numeric characters. I've Googled quite a bit but the results I've come up with dealt with people trying to escape certain characters. My hope is that another set of eyes can see what mine are missing.
Based on the JSON you've pasted in above the error is the usage of single quotes for the value of "password" which is 'xxxxxxxxxxxx'.
It needs to be in double quotes so change it to be:
"password":"xxxxxxxxxxxx",
If you control the code that sets the input go change that otherwise you will have to handle changing the single quote usage possibly through this answer (which is JS but demonstrates the concept), Parsing string as JSON with single quotes?

Why wasn't this json property name the same when originally copied and when erased and written again?

I needed to process this JSON in C#, but even after creating a helper class to which I serialized the json-object using Newtonsoft's [JsonProperty("First name")] tag for the class properties, the json was not serialized correctly. Specifically, the C#-object had null values in its FirstName and LastName properties after serialization.
{
"attributes":{
"First name":"John",
"Last name":"Doe"
}
}
However, this is not a question on Newtonsoft or C#, because those weren't the problem; Originally, I had just copied the json from a colleague in Teams, and then pasted it as the input to my function, and the error persisted. Then I thought (for no apparent reason) to erase the property names in the copied json and write them again, exactly like they were, and the serialization started working fine.
In the code snippet is the original json copied from Teams (with the values changed and irrelevant properties removed). I don't know how it could matter but I think the colleague uses macOS (where the line endings (which I have no knowledge of) are different).
My question is: how was this possible and what information is carried over when copying and pasting text around?
The code string displayed in your question is not valid JSON, although it would be allowed in JavaScript. There is an unexpected comma character at the end of the second object member. It should be:
{
"attributes":{
"First name":"John",
"Last name":"Doe"
}
}
You can make use of the online JSON Validator to catch this error by yourself.
JSON syntax is quite strict, as described in the official page: Introducing JSON. In an object (or an array), commas are separators, not terminators.

What is the correct syntax to fold a JSON string?

I am using Delphi 2009 to build up a string variable containing a simple JON string from values I get from a database. This results in a string of the form below (although the real string could be much longer)
{"alice#example.com": {"first":"Alice", "id": 2},"bob#example.com": {"first":"Bob", "id":1},"cath#example.com": {"first":"Cath", "id":3},"derek#example.com": {"first":"Derek", "id": 4}}
This string gets sent as a header called Recipient-Variables in an email to a company.
The instructions I have for sending the emails to the company say
Note The value of the “Recipient-Variables” header should be
valid JSON string, otherwise we won’t be able to parse it. If
your “Recipient-Variables” header exceeds 998 characters,
you should use folding to spread the variables over multiple lines.
I have looked at these SO posts to try to understand what is meant by folding but cannot really understand the replies as they often seem to be referencing a particular editor.
notepad++ user defined regions with folding
Folding JSON at specific points
Can you customize code folding?
Please can somebody use my example to show me what syntax I should use or what characters I need to insert in my string to comply with the instruction and fold my JSON string, say in between the records for bob and cath?
(BTW I understand what is meant by folding when viewing JSON or other code in an editor but I don't understand how a simple JSON string needs to be formatted in order for the folding to happen at a specific place)
I finally found the answer myself so posting here to help others, just in case.
The answer is given in this document on rfc2822 standards, published in 2001 by the Network Working Group (P. Resnick, Editor)
https://www.rfc-editor.org/rfc/rfc2822#page-11
The document ...
specifies a syntax for text messages that are sent between computer
users, within the framework of "electronic mail" messages.
...and in particular describes how emails are constructed and in particular how to deal with long headers.
Section 2.2.3 talks about long header fields, > 998 characters, and says such headers need to be folded by inserting the CRLF characters followed immediately by some white space, eg a space character.
If the receiving server is following the same standards it will strip out the CRLF character before parsing the header, which will itself will include stripping space characters.
Though structured field bodies are defined in such a way that
folding can take place between many of the lexical tokens (and even
within some of the lexical tokens), folding SHOULD be limited to
placing the CRLF at higher-level syntactic breaks. For instance, if
a field body is defined as comma-separated values, it is recommended
that folding occur after the comma separating the structured items in
preference to other places where the field could be folded, even if
it is allowed elsewhere.
Later, in section 3.2.3 it explains how comments may be combined with folding white space.
So it seems that if generating the string through code, it is necessary to fold long header lines by detecting a higher level syntactic boundary, such as a comma, that is less than 988 characters from the start of the header (or the last fold point) and insert the three hex characters x0D0A20. This could be done after the header has been constructed or on the fly as it is generated.
As a follow up, I now notice that the Overbytes ICS component I am using (TSslSmtpCli) has a boolean property FoldHeaders so this might do all the work for me.

Is it true that a JSON document will not be parseable until the last byte is written?

Is the following hypothesis valid?
Leaving aside whitespace, once the first character of a JSON document
has been written, the resulting stream will not parse as valid JSON
until the last character has been written.
I'm interested in using this assumption so that when I have one process writing a file and another reading it, I can safely ignore partially-written files by ignoring anything that doesn't parse as valid JSON.
I'm sure it depends on the parser you are using... it seems than any scrupulous parser would follow that rule due to the structure of JSON... curly brackets around every "object" key/value pair, including any wrapping document { }).
As always with programming, test rather than assume.