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 last month.
Improve this question
Say your XML file is:
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
</CD>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
</CD>
If you transform it into JSON, should it be (version 1):
{
"CATALOG": {
"CD": [
{
"ARTIST": "Bob Dylan",
"TITLE": "Empire Burlesque"
},
{
"ARTIST": "Bob Dylan",
"TITLE": "Empire Burlesque"
},
]
}
}
Or (version 2):
{
"CATALOG": [
{
"CD": {
"ARTIST": "Bob Dylan",
"TITLE": "Empire Burlesque"
}
},
{
"CD": {
"ARTIST": "Bob Dylan",
"TITLE": "Empire Burlesque"
}
}
]
}
My feeling is that version 1 is more correct but I'm wondering if there is a norm?
Thanks for your feedback - Christian
It depends on what you mean by more correct.
Both Version 1 and Version 2 JSON formats above are valid under RFC 8259.
Read more about it here.
Validate your JSON with it here.
RFC 8259 is the official and standard document used by developers to
develop JSON SDK/ Libraries in different programming languages.
However, with that said, most online XML to JSON converters (like this one here) would handle the conversion with Version 1. Making it more compact and readable (and easy to consume).
When the code is entered in the XML box it applies some rules to
convert XML:
The namespace from the code is removed from the resulting property.
The attribute of the code will be counted as a JSON property.
The sequenced rows in the XML are translated into a JSON array.
There is no correct answer and no standard.
The best answer depends on the semantics of the data, which no tool is likely to know. We can guess the semantics of your data, because we are familiar with words like "artist" and "title". But even knowing that, we have to make guesses. Will all the things in a catalog be CDs, as in your example, or is this just a special case? Is the order of CDs in the catalog significant? Is the order of ARTIST and TITLE significant? Might there be CDs with multiple ARTIST children in the XML, and if so, would you want the single-artist case to use the same structure?
The ideal conversion works from the data model implemented by the XML -- which takes into account questions such as whether a CD has multiple artists and whether the order of their names is signficant -- and then designs a JSON data structure to match that data model. Inferring a data model from one example data-set involves a lot of guesswork.
Related
I have a data-structure similar to the given below, which I am supposed to process. I am designing an API which should accept a POST request similar to the one given below. (ignore the headers, etc)
{
"Name" : "Johny English",
"Id": "534dsf",
"Message":[
{
"Header":"Country of origin",
"Value":"England"
},
{
"Header":"Nature of work",
"Value":"Secret Agent/Spy"
}
]
}
Some how I do not feel, its a correct way to pass/accept data. Here I am talking about structured data vs. Key-Value pair. While I can extract the fields ("Name", "Id") directly to an object attributes, but for Key-Value pairs, I need to loop through the collection and compare with strings (eg. "Nature of Work") to extract values.
I searched few sites, looking for any best practices, could not reach into any conclusion. Is there any best practice, suggestions etc.
I don't think you are going to find any firm, evidence based arguments against including a list of key value pairs in your message schema. But that's the sort of thing to look for - people writing about message schema design, and how to design messages to support change, and so on.
As a practical matter, there's not a whole lot of difference
{
"Name" : "Johny English",
"Id": "534dsf",
"Message":[
{
"Header":"Country of origin",
"Value":"England"
},
{
"Header":"Nature of work",
"Value":"Secret Agent/Spy"
}
]
}
or
{
"Name" : "Johny English",
"Id": "534dsf",
"Message": {
"Country of origin": "England",
"Nature of work": "Secret Agent/Spy"
}
}
In the early days of the world wide web, "everything" is key value pairs, because it was easy to describe a collection of key value pairs in such a way that a general-purpose component, like a web browser, could work with it (ie, definitions of HTML forms). It got the job done.
It's usually good to structure your response data the same as what you'd expect the input of the corresponding POST, PUT, and PATCH endpoints to be. This allows record alteration to not require the consuming entity to transform the data first. So in that context, arrays of objects with "name"/"value" fields is much easier to write input validation for.
Whats the tool to generate xml from xsd and the generated xml should contain more than one entries for the repeatable elements? I tried out tools that are available on eclipse and some online tools like xml-generator, but none of these work. They all generate only one entry for the repeatable elements.
Note: I want to convert the generated xml to json, but the xml-json convertor treats the repeatable elements in the xml as an array only if it has more than one entry.
Generating XMLs from XSD can be quite challenging, if only because what people expect to see may not be possible to be captured by an XSD.
QTAssistant (I am associated with it) has quite extensive features when it comes to sample XML creation.
The simplest (and dumbest) one (available by right clicking on the element in the graphical XSD visualizer) is still able to create two elements if the associated maxOccurs is greater than one.
However, the XML may be off: just because one may have named a field dateTime, it doesn't mean the generated text node will be a valid date time value, if the schema defined it as a string. The tool will also only create one of the choices (if your schema uses xsd:choice), etc.
QTAssistant can make use of additional metadata which gives the user ultimate control over the generated samples. It can even create thousands of XMLs by doing combinations captured using metadata items. (You should contact us on the support site if you're interested in these scenarios).
Regarding XML to JSON conversion, QTAssistant can also correctly convert XMLs to JSON for repeating fields. Given this XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<fundamo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:common="http://www.myorg.com/xsd/gen_fin">
<response>
<common:code>code1</common:code>
<common:description>description1</common:description>
</response>
<transaction>
<transactionRef>transactionRef1</transactionRef>
<dateTime>dateTime1</dateTime>
<userName>userName1</userName>
</transaction>
</fundamo>
The corresponding JSON is:
{
"response": {
"code": "code1",
"description": "description1"
},
"transaction": [
{
"transactionRef": "transactionRef1",
"dateTime": "dateTime1",
"userName": "userName1"
}
]
}
You may notice that transaction is an array, even if XML has only one of those elements. This conversion works for valid XMLs, as long as you have a defined XSD for all its content. For the past 2+ years we've been calling it "XSD-aware JSON conversion". It is also possible to define casing conversion strategies (e.g. change upper case to lower case since XML elements tend to be upper case, while JSON "people" prefer them lower case).
If you're in for a free tool or to write your own, I am sure you can use the free evaluation as a source of inspiration to address only the specific features you're interested in.
Background: We have all seen several ways to configure a distributed application. For my purposes, two of them stand out:
Have a massive database that all nodes have access to. Each node knows its own identity, and so can perform queries against said database to pull out the configuration information specific to itself.
Use tailored (i.e., specific to each node) configuration files (e.g., JSON) so that the nodes do not have to touch a database at all. They simply read the tailored config file and do what it says.
There are pros and cons to each. For my purposes, I would like to explore #2a little further, but the problem I'm running into is that the JSON files can get pretty big. I'm wondering if anyone knows a DSL that is well-suited for generating these JSON files.
Step-by-step examples to illustrate what I mean:
Suppose I make up this metalanguage that looks like this:
bike.%[0..3](Vehicle)
This would then output the following JSON:
{
"bike.0":
{
"type": "Vehicle"
},
"bike.1":
{
"type": "Vehicle"
},
"bike.2":
{
"type": "Vehicle"
},
"bike.3":
{
"type": "Vehicle"
}
}
The idea is that we've just created 4 bikes, each of which is of type Vehicle.
Going further:
bike[i=0..3](Vehicle)
label: "hello %i"
label.3: "last"
Now what this does is to name the index variable 'i' so that it can be used for the configuration information of each item. The JSON that would be output would be something like this:
{
"bike.0":
{
"type": "Vehicle",
"cfg":
{
"label": "hello 0"
}
},
"bike.1":
{
"type": "Vehicle",
"cfg":
{
"label": "hello 1"
}
},
"bike.2":
{
"type": "Vehicle",
"cfg":
{
"label": "hello.2"
}
},
"bike.3":
{
"type": "Vehicle",
"cfg":
{
"label": "last"
}
}
}
You can see how the last label was overridden, so this is a way to sparsely specify stuff. Is there anything already out there that lets one do this?
Thanks!
Rather than thinking of the metalanguage as a monolithic entity, it might be better to divide it into three parts:
An input specification. You can use a configuration file syntax to hold this specification.
A library or utility that can use print statements and for-loops to generate runtime configuration files. The Apache Velocity template engine comes to mind as something that is suitable for this purpose. I suggest you look at its user guide to get a feel for what it can do.
Some glue code to join together the above two items. In particular, the glue code reads name=value pairs from the input specification, and passes them to the template engine, which uses them as parameters to "instantiate" the template versions of the configuration files that you want to generate.
My answer to another StackOverflow question provides some more details of the above idea.
Suppose you have some data you would want business users to contribute to, which will end up being represented as JSON. Data represents a piece of business logic your program knows how to handle.
As expected, JSON has nested sections, data has categorizations, some custom rules may optionally be introduced etc.
It so happens that you already a vision of what "a perfect" JSON should look like. That JSON is your starting point.
Question:
Is there a way one can take a (reasonably complex) JSON and present it in a (non-JSON) format, that would be easy for a non-technical person to understand?
If possible, could you provide an example?
What do you think of this?
http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=90357&av=126401
Or, make your own using Ext JS for the visualization part. After all, JSON is a lingua franca on the web these days.
Apart from that, you could use XML instead of JSON, given that there are more "wizard" type tools for XML.
And finally, if when you say "business users" you mean "people who are going to laugh at you when you show them code," you should stop thinking about this as "How do I make people in suits edit JSON" and start thinking about it as "How do I make a GUI that makes sense to people, and I'll make it spit out JSON later."
Show them as key, value pairs. If your value has sub sections then show them as drill downs/tree structure. An HTML mockup which parses a JSON object in your system would help in the understanding.
Picked this example from JSON site
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
Name,format would be the tree nodes.
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
What is the JSON (JavaScript Object Notation) format?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
Ref.: json.org
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
(source: json.org)
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
(source: json.org)
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
(source: json.org)
A string is a collection of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
(source: json.org)
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
(source: json.org)
Here is an example:
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [{
"onclick": "CreateNewDoc()"
}, {
"value": "Open",
"onclick": "OpenDoc()"
}, {
"value": "Close",
"onclick": "CloseDoc()"
}]
}
}
}
And in XML the same thing would have been:
<menu id="file" value="File">
<popup>
<menuitem value="New" onclick="CreateNewDoc()" />
<menuitem value="Open" onclick="OpenDoc()" />
<menuitem value="Close" onclick="CloseDoc()" />
</popup>
</menu>
Ref.: json.org
Hope you now get an idea of what is JSON.
From Wikipedia: JSON (Javascript object notation)
The JSON format is often used for
transmitting structured data over a
network connection in a process called
serialization. Its main application is
in Ajax web application programming,
where it serves as an alternative to
the use of the XML format.
The in-depth version seems to be well covered, maybe you're looking for the short-and-simplified version?
JSON is basically just a way to pass an array from one language to another.
It's used a lot for Ajax (amongst other things) because with Ajax you typically have a server-side language (PHP etc.) passing a set of results to a client-side language (javascript). Your javascript calls your PHP page with some parameters; your PHP page builds an array and echos it encodes it to JSON format; your javascript catches the JSON and decodes it back to an array to process.
There's more to it than that obviously (and for that reason I'm expecting a flurry of tear-streaked downvotes :) ), but that's all you need to get up and running with it.
It is JavaScript Object Notation. You can use it to send data back and forward. It is often recommended since there is not so much overhead, like the one you get with XML. This is why it has become more popular than XML with Ajax.
Take a look at this:
http://en.wikipedia.org/wiki/JSON
In my opinion when one wants to access webservice of different service provider such as Twitter,facebook etc over HTTP.
Then one must create a url and request for connection .When connection established a large amount of data comes from requesting site .
Example
<7b226665 65644974 656d7322 3a5b7b22 63617465 676f7279 223a7b22 6e616d65 223a2254 72616e73 706f7274 6174696f 6e222c22 68656164 65725f69 636f6e22 3a225c2f 686f6d65 5c2f6164 6d696e5c 2f707562 6c69635f 68746d6c 5c2f7072 6f647563 74696f6e 2e6d6973 73696f6e 7a65726f 2e6f7267>
This DATA is too much difficult to understand and arbitary in nature so we have 2 option for the representation of arbitrary data structures either in JSON format or XML format .But disadvantage in XML , it is syntactically more complex and bigger in file size than JSON. So it is better to use JSON
JSON: JavaScript Object Notation.
JSON is a syntax for storing and exchanging data.
JSON is an easier-to-use alternative to XML.
JSON is a lightweight data-interchange format
JSON is language independent *
JSON is "self-describing" and easy to understand
EXAMPLE:
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}