Which of the following are valid JSON documents? - json

Which of the following are valid JSON documents?
{“name”:”Fred Flintstone”;”occupation”:”Miner”;”wife”:”Wilma”}
{“title”:”Star Wars”, “quotes”:[“Use The Force”,”These are not the Droids you are looking for”],”director”:”George Lucas”}
{}
{“city”:”New York”, “population”, 7999034, boros:{“queens”, “manhattan”, “staten island”, “the bronx”, “brooklyn”}}
{“a”:1, “b”:{“b”:1, “c”:”foo”, “d”:”bar”, “e”:[1,2,4]}}

This is obviously homework, so I will try to help you to come to the correct solution yourself, not just hand it to you.
Look up which character is used to separate the individual key/value pairs from each other in a JSON document. One of the documents is using the wrong character.
Look up the difference between objects and arrays in JSON. What's the difference and which characters are used to mark the beginning and end of either? The author of one of the documents tried to create an array, but uses the syntax for objects.
The official JSON specification can serve as a reference for you.

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.

Specifiy type when converting from XML to JSON in MarkLogic

Using MarkLogic 8, I'm using a custom XML to JSON conversion for json:transform-to-json, and I've got it working just about right except the conversion is outputting numbers as strings.
Is there a way to specify that the value of a particular element should be a number value, not a string?
I don't see anything in the doc for json:config, but just in case there's something I've missed, or if you have a neat post-processing trick, I'd love to hear about how to solve this problem.
You can do that by defining an XML Schema for the non-string type elements. Just make sure it is available in the context (by loading it into xdmp:schemas-database()), and that it is recognized (your XML needs to have a namespace that matches the XML Schema, and you might wanna use import schema)..
HTH!

Delimiter for multiple json strings

I'd like to save multiple json strings to a file and separate them by a delimiter, such that it will be easy to read this list in, split on the delimiter and work with each json doc separately.
Serializing using a json array is not an option due to external reasons.
I would like to use a delimiter that is illegal in JSON (e.g. delimiting using a comma would be a bad idea since there are commas within the json strings).
Are there any characters that are not considered legal in JSON serialized strings?
I know it's not exactly what you needed, but you can use this SO answer to write the json string to a CSV, then read it on the other side by using a good streaming CSV reader such as this one
NDJSON
Have a look at NDJSON (Newline delimited JSON).
http://ndjson.org/
It seems to me to be exactly how you should do things, though its not exactly what you asked for. (If you can't flatten your JSON objects into single lines then it's not for you though!) You asked for a delimiter that is not allowed in JSON. Newline is allowed in JSON, but it is not necessary for JSON to contain newlines.
The format is used for log files amongst other things. I discovered it when looking at the Lichess API documentation.
You can start listening in to a broadcast stream of NDJSON data part way through, wait for the next newline character and then start processing objects as and when they arrive.
If you go for NDJSON, you are at least following a standard and I think you'd be hard pressed to find an alternative standard to follow.
Example NDJSON
{"some":"thing"}
{"foo":17,"bar":false,"quux":true}
{"may":{"include":"nested","objects":["and","arrays"]}}
An old question, but hopefully this answer will be useful.
Most JSON readers crash on this character: , which is information separator two. They declare it "unexpected token", so I guess it has to be wrapped to pass or something.

Parse string as Json?

I have a json string but the format is strange
[[5706,[
[1132001,"Aston Villa","West Ham",5706,'2010-08-14T15:00:00.0000000',[[1966752,0],[1977724,1]],[],"","","","",64,63],
[1132002,"Blackburn","Everton",5706,'2010-08-14T15:00:00.0000000',[[1966753,0],[1977721,1]],[],"","","","",83,66],
[1132004,"Bolton","Fulham",5706,'2010-08-14T15:00:00.0000000',[[1966755,0],[1977727,1]],[],"","","","",88,85],
[1132005,"Chelsea","West Brom",5706,'2010-08-14T15:00:00.0000000',[[1966756,0],[1977712,1]],[],"","","","",503,94],
[1132006,"Liverpool","Arsenal",5706,'2010-08-14T15:00:00.0000000',[[1966757,0],[1977733,1]],[],"","","","",59,58],
[1132007,"Man Utd","Newcastle",5706,'2010-08-14T15:00:00.0000000',[[1966758,0],[1977730,1]],[],"","","","",19,70],
[1132008,"Sunderland","Birmingham",5706,'2010-08-14T15:00:00.0000000',[[1966759,0],[1977715,1]],[],"","","","",60,86],
[1132009,"Tottenham","Man City",5706,'2010-08-14T15:00:00.0000000',[[1966760,0],[1977739,1]],[],"","","","",62,77],
[1132010,"Wolves","Stoke City",5706,'2010-08-14T15:00:00.0000000',[[1966761,0],[1977718,1]],[],"","","","",81,342],
[1146485,"Wigan","Blackpool",5706,'2010-08-14T15:00:00.0000000',[[1998244,0],[1998321,1]],[],"","","","",350,328]],
[[1015824,8],[1035605,126]]]]
So, How can i parse and extract it value?
Thanks
Just eval it using whatever library is relevant to the language you're parsing it into. The format is a list of values and lists - so from the resulting structure you'll just need to traverse the lists to get to the pieces that you're interested in. Without more context, I can't provide any brilliant insight into what the values mean in those lists, but doing a little googlin' digging makes me thing you're looking at data related to football club tickets and/or games.

JSON - not all fields quoted in Dojo diji.tree sample code in book

O'Reilly book "Dojo - The Definitive Guide" page 378 shows the following sample Tree structure which is supposedly JSON. It seems to work in building the Dijit Tree structure.
{
identifier: 'name',
label:'name',
items: [
{
name: "Programming Languages",
children: [
etc...
Should the word identifier, label, items, name, and children be enclosed in quotes?
I'm writing a Python program to generate syntax that is compatible with their desired tree structure. Just to test my output, I tried:
testDict = "xxxx" where xxxx is the string supposed JSON string above.
It always gives an error that 'identifier' is not defined.
So I'm curious if this was a typo - or if there are some new keywords or features of JSON that I need to learn.
Thanks,
Neal Walters
JSON doesn't really have any additional features. That's the beauty of it :)
You don't have to wrap those names in quotes. The names before the colon are supposed to be quoted, strictly according to the JSON spec. Why? Mostly (only?) because JavaScript gets upset when reserved words are used as object properties -- for example, if you had properties called 'function' or 'return'. Quoting these names consistently avoids this problem. Dojo doesn't care. It just uses eval to parse the JSON, and as long as you avoid keywords, it won't enforce the use of quotes. You can use quotes consistently if you like to be JSON compliant.
I'm not sure exactly what problem you had with your testDict example. I don't fully understand the context (what is testDict, what language are you using to set up that string, how is it used, etc.) Perhaps you needed to escape something in the JSON such as nested double quotes?
These are not new keywords or features of JSON but they are how dojo expects a JSON file to be structured. You should wrap them in quotes. Here's an example from dojocampus.