Why doesn't JSON tolerate trailing commas after the last element? [closed] - json

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Why doesn't JSON tolerate a trailing comma after the last element?
[
{
"id": 1,
"username": "john"
},
{
"id": 2,
"username": "william"
}
]
Be it a comma after the value "John" or be it the second JSON user object, why is JSON not tolerating the comma? Don't we have a simple engineering solution that can solve this tiny problem and by the way we are in the "Deep Learning" era. I am more curious to know why does it exist.

An array structure is represented as square brackets surrounding
zero or more values (or elements). Elements are separated by
commas.
array = begin-array [ value *( value-separator value ) ] end-array
There is no requirement that the values in an array be of the same
type.
RFC 7159 section 5
Effectivly, trailing commas are not allowed as defined by the specification.
There are linting tools which will automagically fix it for you, but it's usually a sign you've done something wrong.

Related

Unmarshalling nested json with multiple type [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
[
1618511472,
"on-req",
null,
null,
[
[
62577595747,
]
],
]
Above is the json response that needs to be parsed
I'm currently using var resp []interface{} and json.Unmarshal for the raw response. It works for other fields except the array field
When I try to cast the array field resp[4].([][]interface{}) it doesn't work
Not sure what is the best approach here. Appreciate any help. Thanks beforehand
Thanks to mkopriva's answer
This is the working solution
tmp, ok := resp[4].([]interface{})
tmp1, ok1 := tmp.([]interface{})

unable to parse a json string using JSON.parse? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to parse a json string in my code , how ever I am getting an error as below.
SyntaxError: Unexpected token i in JSON at position 2.
here is my code that tries to parse the string. this piece of code is written in my angular project .
JSON.parse('{ "id": "id", "lastName": "prasanth", "firstName": "mp", "password": "bingo!!!", "passwordInHistory": "true" }'))
there's one too many parentheses at the end
try this instead:
JSON.parse('{ "id": "id", "lastName": "prasanth", "firstName": "mp", "password": "bingo!!!", "passwordInHistory": "true" }')

How to validate two JSON objects present in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a JSON String to validate which contains two separate objects. The string is "1A" but i want to validate it as individual objects for example: {"NumberInt":1,"LetterThing":"A"}.
In conclusion, my string is "1A" but I need to validate it as individual objects despite the fact it's in a string format.
Why do I want this? I have a minimum and maximum for the NumberInt integer value and I have a particular pattern for the LetterThing string value. For example: I do not want "5H" to validate.
If this is possible in a string format please let me know how.
Solved:
Was solved by using regex to validate on my JsonSchema i.e "pattern": "^[A-Ja-j1-4\\s]*$".
Thanks guys
You could use a regex to extract what you need from the JSON.
//obtains the number part, then you can perform operations on that number
var startingDigits = incomingString.replace( /^\D+/g, '');
You would need to PARSE the string in this case.
To parse a STRING you ITERATE over each CHARACTER in the string and then compose the needed parsed ELEMENTS.
For example in this CASE you might start looking for only DIGITS and putting them in another string. When you hit a LETTER you can CONVERT that string to an integer.
Then take the REMAINING as the 2nd part.
Finally do your VALIDATION.

Mapping map field to Ember Data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have response from server in this format :
{
"id":"552cd25444ae47fe5f3a41b2",
"eventType":"REST_EVENT",
"userSid":"REST_USER",
"content":{
"key":"value",
"key1":"value1",
"key2":"value2"
}
}
How I can map content field to Ember Data Model if this field has dynamic count of values and without knowing key names ?
Its really simple. Use this in your model
content : DS.attr()
Ember will automatically pick up if you pass array, string or number. In your case array.
You can even iterate over it. Watch it using Observers like any other array or model attribute in Ember.

What is the advantage of N-Triples over CSV for storing RDF triples? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
N-Triples is a line based serialization format for an RDF graph. Each line represents the subject, predicate and object of an RDF Triple separated by whitespace and ended with a dot like:
<http://one.example/subject1> <http://one.example/predicate1> <http://one.example/object1> .
More details can be found here: http://www.w3.org/TR/n-triples/
But why is it necessary to define such a format, if one could serialize RDF Triples simply using CSV like
http://one.example/subject1, http://one.example/predicate1, http://one.example/object1
I could even easily extend to support N-Quads, N-Quints, ... using CSV. What are the advantages of N-Triples over CSV for serializing RDF triples?
Disclaimer: I'm the original editor of N-Triples and implemented it in Raptor http://librdf.org/raptor/ both the N-Triples original and the 2013 version.
There are multiple answers to this but it's basically ambiguity. CSV can't distinguish between a URI that looks like http://foo.com/ and a string http://foo.com/
In CSV
http://foo.com/,http://foo.com/,http://foo.com/
this could be a triple
(URI http://foo.com/, URI http://foo.com/, URI http://foo.com/)
or
(URI http://foo.com/, URI http://foo.com/, Literal http://foo.com/)
N-Triples adds <> and "" for distinguishing these cases