var xml = @"
<Invoice>
<Timestamp>1/1/2017 00:01</Timestamp>
<CustNumber>12345</CustNumber>
<AcctNumber>54321</AcctNumber>
</Invoice>";
Posts tagged with 'c'
This is a repost that originally appeared on the Couchbase Blog: XML to JSON conversion with Json.NET.
XML data can be converted to JSON, which can be loaded into Couchbase Server (Couchbase Server 5.0 beta now available). Depending on the source of the data, you might be able to use a tool like Talend. But you may also want to write a simple C# .NET application with Newtonsoft’s Json.NET to do it.
XML data
For the purposes of this tutorial, I’m going to use a very simple XML example. If your XML is more complex (multiple attributes, for instance), then your approach will also have to be more complex. (Json.NET can handle all XML to Json conversions, but it follows a specific set of conversion rules). Here’s a sample piece of data:
Notice that I’ve got this XML as a hardcoded string in C#. In a real-life situation, you would likely be pulling XML from a database, a REST API, XML files, etc.
Once you have the raw XML, you can create an XmlDocument
object (XmlDocument
lives in the System.Xml
namespace).
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
Conversion with Json.NET
Once you have an XmlDocument
object, you can use Json.NET to convert that object into a Json representation.
var json = JsonConvert.SerializeXmlNode(doc, Formatting.None, true);
In this example, I’m asking Json.NET to serialize an XML node:
-
I used
Formatting.None
. If I wanted to display the actual Json, it might be better to useFormatting.Indented
-
The last
true
specifies that I want to omit the root object. In the XML above, you can think of<Invoice></Invoice>
as the root object. I just want the values of the Invoice object. If I didn’t omit the root node, the resultant Json would look like:{"Invoice":{"Timestamp":"1/1/2017 00:01","CustNumber":"12345","AcctNumber":"54321"}}
Saving the Json result
Finally, let’s put the Json into Couchbase. The easiest way to do this would be to again call on JsonConvert
to deserialize the Json into a C# object
. That object would then be used with Couchbase’s bucket.Insert(…)
method.
object transactObject1 = JsonConvert.DeserializeObject(json);
bucket.Insert(Guid.NewGuid().ToString(), transactObject1);
With this method, the Json would be stored in Couchbase like so:
That might be fine, but often times you’re going to want more control of the format. With Json.NET, we can serialize to a given class, instead of just object
. Let’s create an Invoice
class like so:
public class Invoice
{
public DateTime Timestamp { get; set; }
public string CustNumber { get; set; }
public int AcctNumber { get; set; }
}
Notice that there is some type information now. The Timestamp is a DateTime
and the AcctNumber is an int
. The conversion will still work, but the result will be different, according to Json.NET’s conversion rules. (Also check out the full Json.NET documentation if you aren’t familiar with it already).
Invoice transactObject2 = JsonConvert.DeserializeObject<Invoice>(json);
bucket.Insert(Guid.NewGuid().ToString(), transactObject2);
The result of that insert will look like:
-
Notice that the timestamp field is different: it’s stored in a more standardized way.
-
The acctNumber field value is not in quotes, indicating that it’s being stored as a number.
-
Finally, notice that the field names are different. This is due to the way Json.NET names Json fields by default. You can specify different names by using the
JsonProperty
attribute.
That’s it
One more minor thing to point out: I used Guid.NewGuid().ToString()
to create arbitrary keys for the documents. If you have value(s) in the XML data that you want to use for a key, you could/should use those value(s) instead.
This blog post was inspired by an email conversation with a Couchbase user. If you have any suggestions on tools, tips, or tricks to make this process easier, please let me know. Or, contact me if there’s something you’d like to see me blog about! You can email me or contact me @mgroves on Twitter.
Ed Finkler is open-sourcing mental illness.
Show Notes:
- OSMI
- OSMI's external resources, which includes books and links to other material
- Steve Andrews was the speaker at KalamazooX
- Episode 15 of the Development Hell podcast
- The ad that we discussed that says "I rarely get to see my kids. That's a risk you have to take." as an example of romanticizing overwork / workaholism. It's also tweeted by Jason Fried, one of the founders of basecamp.
- Ed Finkler will/has spoken at:
- WordCamp DC 2017
- WordCamp Minneapolis 2017
- PyOhio in Columbus, Ohio (which is right in my backyard and I've STILL never been to! Shame on me!)
Want to be on the next episode? You can! All you need is the willingness to talk about something technical.
Theme music is "Crosscutting Concerns" by The Dirty Truckers, check out their music on Amazon or iTunes.
This is a repost that originally appeared on the Couchbase Blog: Documentation Contribution and Improvements.
Documentation is undergoing some changes for the better at Couchbase. Matt Carabine and the documentation team have been working to improve the build process, backporting, and review. And, they have made it easier to contribute to documentation in the process.
Contributing to the documentation
Couchbase documentation is open source, but until recently, there were a number of problems. If you were looking to make a substantial change or maybe just fix a quick typo, you were faced with one or more of these questions:
-
Where do I look to make a change?
-
What file do I change?
-
What file maps to the doc page I’m looking at?
-
How do I change it?
It was not always clear how to go from a documentation URL to a specific file in the Github repository.
Now, that’s changed. If you visit a page in the documentation, you will notice an "Edit on GitHub" link on each page. Click this link and you’ll be taken to GitHub to edit the file directly (your first time, you will be prompted to create a fork).
Example
First, visit the Using Graceful Failover page in the documentation.
Next, click the "Edit on GitHub" link.
At this point, you’re on GitHub. Assuming you’ve used Git/GitHub before, the rest of this process should be familiar.
If you’ve not already created a fork, you will be prompted to do so.
After that, you will see an edit screen where you can make your change.
Once you’ve made a change, click the "Propose file change". You’ll see a diff, and you’ll be able to create a pull request.
Contribution guide
That’s a crash course on contributing to the documentation. There’s a full guide on how to contribute to Couchbase Server docs.
Note that while that guide is written in Markdown, Couchbase Server documentation is written in the Darwin Information Typing Architecture (DITA) markup language.
For simple typo corrections, you may not need to know much about DITA, but for deeper edits, you will need to familiarize yourself with the syntax and/or an editing tool like Oxygen.
Documentation contributions
Thank you to those in the community who have already contributed! This new system has only been going for a few months, but we’ve already gotten some great contributions. So I tip my hat to:
-
eunosm3 for #1180 - better shortdesc for manage caching layer architecture
-
atom992 for #1365 - correcting a flag for cbexport
-
Sir4ur0n for #1366 - fixing a N1QL typo
-
ptorsson for #1414 - correcting some Go SDK examples
-
MarkTickner for #1457 - adding docs for the REST API services parameter
-
ecejnj42 for #1585 - fixing misplaced options in durability area
-
rabdill for #1632 - fixing a typo in Go N1QL examples
-
GauthamBanasandra for #1696 - fixing some grammer in the subdocuments area
-
oxyrax for #1725 - correcting a setting for cbbackupmgr
It astounds me that we had such great contributions from so many people so quickly! Thank you, everyone, for helping to make the documentation better.
How did we get here?
To make contribution easier, the build process had to be improved. Previously, it involved a bunch of manual steps: pull source, build locally, zip up the results, FTP them to a server, run one or more slow build server tasks manually. Now you’ll see that the build step happens much more quickly in the pull request, and previews are generated quickly.
The reviewing of content was also improved. Previously, previews were only possible on a single location. There was no simultaneous reviewing possible. This, combined with the slow, manual builds, was frustrating, and made it difficult to review all changes, let alone external ones.
Finally, backporting to other versions of Couchbase Server documentation was also a problem. The documentation is stored in separate branches for each version, so if an issue is raised and fixed in 4.6, it may also apply to 4.5, and so on. That’s a very manual process, but a @cb-docs-robot was created to automate the backporting (which you’ll see in many of the above examples).
How are we doing?
Another thing we’ve added is the ability to give us feedback on the documentation right from your browser.
Simply find the "Feedback On This Page" button at the bottom right of your browser, click it, and write feedback.
Summary
These documentation improvements help us both internally and externally. We hope that it will make your experience with the documentation less frustrating and more enjoyable. We welcome your feedback! Please check out the Couchbase docs on GitHub.
Peter Piekarczyk is concerned about burnout, stress, and anxiety.
Show Notes:
- Original tweet that lead to this episode
- OSMI - watch for an episode featuring Ed Finkler soon!
- "Developers love Medium". (I'm not sure this developer does :)
- React Native Radio podcast
- Peter is on Github
Peter Piekarczyk is on Twitter
Want to be on the next episode? You can! All you need is the willingness to talk about something technical.
Theme music is "Crosscutting Concerns" by The Dirty Truckers, check out their music on Amazon or iTunes.
Brett Whittington is concerned about security on data in motion.
Note: I said "SSH" at one point, I meant SSL; Brett was too polite to point it out. I also made a mustard pun. Please send your hate tweets to @spetryjohnson.
Show Notes:
- SSL Labs - SSL Server Test
- ZAPP from OWASP
- Jim Manico ("AppSec Enthusiast") on Twitter
- The DROWN attack
- Heartbleed
- Google's collision attack on two different documents
- 0 Day Exploit exposed by Wikileaks
- Innovative Codes explaining how HTTPS works
- J Wolfgang Goerlich ("hacker strategist") on Twitter
Brett Whittington is on Twitter
Want to be on the next episode? You can! All you need is the willingness to talk about something technical.
Theme music is "Crosscutting Concerns" by The Dirty Truckers, check out their music on Amazon or iTunes.