How to convert CSV to JSON in PHP

In this example I am going to show you how to convert CSV to JSON using PHP programming language. CSV stands for comma separated values and JSON stands for JavaScript Object Notation. These are actually data formats used for representations. Having data formats in CSV and JSON are advantageous because of their light-weight, easy to representation and easy to parse. JSON format is currently widely used in REST response.

Continue

How to return Different Data Formats (JSON, XML) from Spring REST API

Here I am going to show you an example on how to return different data formats from REST API using Spring Boot framework. You may have a requirement that you need to return multiple formats, such as XML, JSON, etc., from the REST response, then this example may help you. This example will return XML and JSON format depending on your HTTP settings. By default the REST will send you the JSON response. You need to set key Accept‘s value in HTTP Headers to text/xml or application/xml to get the…

Continue

MongoDB Aggregation Operations

You are not going to always return records from MongoDB but sometimes you need to perform some computations such as sum, count, average, etc. Here you need the aggregation operations which are used to process data that return the computed results. Therefore aggregation actually groups data from multiple documents and performs a variety of operations on the grouped data to return the single result. Aggregate operations retrieve a single value after performing a calculation on a set of values. Often aggregate functions are accompanied by group clause in the find()…

Continue

MongoDB find() method with Projection

You may not want to fetch a whole bunch of data from the document which is stored in a collection in your MongoDB database. You can restrict the number of records or data using different conditions with the help of WHERE, AND, OR and IN operators, but what if you want to restrict the data you want in your particular row or record or document. So here comes the projection that helps you to selectively choose data from a record or row or document. In MongoDB, projection means selecting only…

Continue

How to query documents – WHERE, AND, OR, IN conditions in MongoDB

MongoDB is one of the most popular NoSQL database where data are stored in the form of documents. You can also create tables in traditional way to put your structured data but the main purpose is to store unstructured data in the form of object which may vary based on a particular requirement. Now when you select data from collection, you may not need all data to be fetched from the database collection and you want to restrict those data based on certain criteria or conditions while fetching from database.…

Continue

How to make output of MongoDB’s find() readable in shell

MongoDB is one of the most popular NoSQL database where your data are stored in a collection. To quickly access your data from MongoDB you can execute commands on MongoDB shell and your results appear based on the given criteria. One of the commands is find() function and using this command you can easily retrieve data from MongoDB collection. When you execute this find() command to retrieve data from a collection, it displays data in one line which makes it ugly and become difficult to read specially when there multiple…

Continue

How to pretty print JSON in Java

Here I am going to show you how to pretty print JSON in Java. I am going to use here JSON, Jackson and Google Gson library to prettify the JSON. I am also going to show you how to read and write JSON file using Java 8 or later version. I am going to show you how to prettify JSON string as well as JSON file data. Why do you need to prettify the JSON data? Because you want to read this JSON data manually when you received this data…

Continue

How to load local JSON File in Angular Application

JSON, i.e., JavaScript Object Notation is a lightweight format for interchanging data. It is based on a subset of JavaScript language. For example, this JSON data format is used in the webservice response. In old days webservices used XML for the primary data format but nowadays JSON, being lightweight, is becoming the preferred data format for exchanging data for most of the webservices. JSON is built on two structures: A collection of name/value pairs. In various languages, the JSON structure is realized as an object, record, struct, dictionary, hash table,…

Continue

Sanitize JSON using Java

Introduction Here I am going to tell you how to sanitize JSON (JavaScript Object Notation) string. Given JSON-like content, convert it to valid JSON. The OWASP JSON Sanitizer Project is a simple to use Java library that can be attached at either end of a data-pipeline to help satisfy Postel’s principle: be conservative in what you do, be liberal in what you accept from others. When applied to JSON-like content from others, this project will produce well-formed JSON that should satisfy any parser you use. When applied to your output…

Continue