• Skip to main content
  • Skip to primary sidebar

CodeBlogMoney

Make Money using Coding and Blogging

JSON

Create a JSON File : 3 easy ways

June 21, 2020 by Jimmy

How to create a JSON File?

How to create JSON file. This article helps to create valid JSON document File.
Create a valid JSON file

To create JSON file first we need to understand what is JSON and what it represents. Please check out this link to find more about the JSON What is JSON?

Once we understand JSON, there are 3 ways to create a new JSON.

  1. Using Text Editor
  2. Using Online Tools such as https://jsonformatter.org
  3. Create a file from the JSON URL

1. Using Text Editor

Open a Text editor like Notepad, Visual Studio Code, or Sublime or your favorite one.

Cope and Paste below JSON data in Text Editor or create your own base on the What is JSON article.

{
  "InsuranceCompanies": {
    "Top Insurance Companies": [
      {
        "No": "1",
        "Name": "Berkshire Hathaway ( BRK.A)",
        "Market Capitalization": "$308 billion"
      }
    ],
    "source": "investopedia.com",
    "Time": "Feb 2019"
  }
}

Use this JSON validator tool to validate the JSON

https://codebeautify.org/jsonvalidator

Once file data are validated, save the file with the extension of .json, and now you know to create the Valid JSON document.

2. Using Online Tool

Open a JSON Formatter tool from the link below

https://jsonformatter.org/

Copy and Paste the JSON Data which is mention in Option 1 in the Input tool of the online tool

Jsonformatter.org helps to create , validate and format the JSON Data
Click on Download icon Highlighted in Red from the right textarea

Download this file using the download icon mentioned on the Right input area. It will download the JSON file.

This jsonformatter.org already support the JSON validation. This will create a New JSON file and will allow downloading.

Now if you already have JSON document or file, you can upload it to this tool and modify or delete few objects, array of the JSON and download the updated and valid JSON file

3. create a file from the JSON URL

Developer needs to work with API and nowadays 95% API returns data as JSON.

Here are few samples of the JSON URL. Now click on these JSON samples.

  1. https://gist.githsubusercontent.com/jimmibond/9205480889e19c0de347/raw/sample.json
  2. https://gist.githubusercontent.com/cbmgit/852c2702d4342e7811c95f8ffc2f017f/raw/InsuranceCompanies.json

Once they are open use save as functionality of the browser and save these file.

I hope this article helps you to understand basic ways to create JSON file a Valid and formatter.

Filed Under: JSON

JSON Example with Data Types Including JSON Array

July 4, 2018 by Jimmy

JSON Example

This article will have all the JSON Examples which covers each and every data type JSON supports. Here is the list of JSON data types.

Valid JSON Data Types

  1. String
  2. Number
  3. Object
  4. Array
  5. Boolean
  6. Null

1. JSON String Example:

{
  "firstname": "Tom",
  "lastname": "Cruise",
  "occupation": "Actor"
}

This example shows information about a person, and you know Tom Cruise. This JSON data represent details which contain the string.  Play with JSON String Example. String in JSON has to be in double-quotes.

2. JSON Number Example:

{
  "id": 1,
  "age": 56,
  "bornyear": 1963,
  "date": 3,
  "month": 7,
  "weight" : 67.5
}

This example shows how to represent the numbers and I have added the birthday details of Tom Cruise. I have added this in 2018. So you can do the math if this is not 2018. 🙂 Play with JSON Number Example. Numbers in JSON has to be an integer or a floating-point.

3. JSON Object Example:

{
  "actor": {
    "name": "Tom Cruise",
    "age": 56,
    "Born At": "Syracuse, NY",
    "Birthdate": "July 3 1962",
    "photo": "https://jsonformatter.org/img/tom-cruise.jpg"
  }
}

This JSON data represent the detail about the actor and its property. It is a JSON object which can have different properties. Play with JSON Object Example.

4. JSON Array Example

{
  "Actors": [
    {
      "name": "Tom Cruise",
      "age": 56,
      "Born At": "Syracuse, NY",
      "Birthdate": "July 3, 1962",
      "photo": "https://jsonformatter.org/img/tom-cruise.jpg"
    },
    {
      "name": "Robert Downey Jr.",
      "age": 53,
      "Born At": "New York City, NY",
      "Birthdate": "April 4, 1965",
      "photo": "https://jsonformatter.org/img/Robert-Downey-Jr.jpg"
    }
  ]
}

This is an Array of JSON data. It shows two actor objects and each object can have its own properties. Play with JSON Array Example.

5. JSON Boolean Example

{
  "hasChildren":true,
  "hasTwitterAccount":true,
  "hasGreyHair":false
}

This JSON Example shows how to represent Boolean values in JSON. Play with JSON Boolean Example. Boolean value has to be true or false.

6. JSON Null Example

{
  "wife":null
}

JSON Null Example shows how to represent Null values in JSON. Play with JSON Null Example. Null will help to represent value as not available.

I have explained all the Six JSON data types in the above examples.

Example with all the JSON Data Type.

{
  "Actors": [
    {
      "name": "Tom Cruise",
      "age": 56,
      "Born At": "Syracuse, NY",
      "Birthdate": "July 3, 1962",
      "photo": "https://jsonformatter.org/img/tom-cruise.jpg",
      "wife": null,
      "weight": 67.5,
      "hasChildren": true,
      "hasGreyHair": false,
      "children": [
        "Suri",
        "Isabella Jane",
        "Connor"
      ]
    },
    {
      "name": "Robert Downey Jr.",
      "age": 53,
      "Born At": "New York City, NY",
      "Birthdate": "April 4, 1965",
      "photo": "https://jsonformatter.org/img/Robert-Downey-Jr.jpg",
      "wife": "Susan Downey",
      "weight": 77.1,
      "hasChildren": true,
      "hasGreyHair": false,
      "children": [
        "Indio Falconer",
        "Avri Roel",
        "Exton Elias"
      ]
    }
  ]
}

Play JSON Example with all data types.

I hope this article will help to understand the JSON data. If you want to know more about JSON check out this article. What is JSON?.

 

Filed Under: JSON Tagged With: json

What is JSON? Explained with JSON Examples

July 3, 2018 by Jimmy

What is JSON?

What is JSON? JSON is a way to store, persist data in a structure which is easy to read and easy to access. JSON stands for JavaScript Object Notation, which machine can understand and generate. JSON structure provides a human-readable collection of information to explain and understand the logical representation of data.

Let’s get to the JSON structure.

{
  "Actors": [
    {
      "id": "1",
      "firstName": "Tom",
      "lastName": "Cruise"
    },
    {
      "id": "2",
      "firstName": "James",
      "lastName": "Bond"
    }
  ]
}

This example shows the Actors’ data and has two actors. It’s easy to understand along with that it takes less space to store in memory than XML.

Above JSON data consists of an array of Actors and each actor is value in JSON Array. Looks complicated?

Let’s understand all the data type JSON has, to represent the data.

JSON Data Types

  • Number
  • String
  • Boolean
  • Array
  • Object
  • Null

This example uses all the JSON data types.

{
  "id": 1,
  "age": 30,
  "Name": "James Bond",
  "Address" :[
      {
        "city":"Los Angels",
        "country": "USA"
      },
      {
        "city":"London",
        "country": "UK"
      }
    ],
  "wife":null,
  "ishavingmobile":true,
  "firstmovie" :{
    "name":"Dr. No",
    "releaseyear":1962
  }
 
}

I hope you are having an understanding of JSON now. Let me know if you have any questions about JSON in the comment sections, I am happy to help. You can also play with JSON data at https://jsonformatter.org

Other JSON related articles.

Validate JSON using Javascript

JSON Examples with all Datatypes

Filed Under: JSON

Read JSON File using Python Code and Prompt

May 30, 2018 by Jimmy

Read JSON File using Python requires importing the JSON library of Python. Before we deep dive in code let me brief you about the JSON.

JSON (Javascript Object Notation) is easy to read, write, analyze and flexible string-based format which can be used to serialized (store) and transfer between multiple application, products and between the multiple layers (Client/Server).

I have been playing around with JSON from the last 5 years in Python, Javascript, NodeJS, and JAVA.

Program to Read JSON file using Python:

import json

with open('jsonfile.txt') as jsonfile:
    parsed = json.load(jsonfile)

print json.dumps(parsed, indent=2, sort_keys=True)

Create a jsonfile.txt using this JSON data. a parsed variable will become a dictionary object out of parsing JSON file. Please check in the image, how the parsed object looks like in debug mode. to understand more about “json.dump”, please visit JSON Pretty Print using Python.

Read JSON File using Python
JSON Object in Python IDE Debug

{
  "employees": {
    "employee": [
      {
        "id": "1",
        "firstName": "Tom",
        "lastName": "Cruise"
      },
      {
        "id": "2",
        "firstName": "Maria",
        "lastName": "Sharapova"
      },
      {
        "id": "3",
        "firstName": "James",
        "lastName": "Bond"
      }
    ]
  }
}

Read a Python File on Python Prompt.

>>> import json
>>> jsondata = json.loads(open('jsonfile.txt').read())
>>> jsondata
{u'employees': {u'employee': [{u'lastName': u'Cruise', u'id': u'1', u'firstName': u'Tom'}, {u'lastName': u'Sharapova', u'id': u'2', u'firstName': u'Maria'}, {u'lastName': u'Bon
d', u'id': u'3', u'firstName': u'James'}]}}

If you don’t have python setup and still want to open a JSON file to read. Please visit https://jsonformatter.org and click on an open file, select file from your system and it will open in JSON Editor.

I hope this article will help to read a JSON File using Python. Please share your views on this article.

Filed Under: JSON, Python Tagged With: File, json, python

JSON Pretty Print using Python- with Examples

May 25, 2018 by Jimmy

JSON Pretty Print using Python is required frequently for testing, analyzing and debugging JSON data. Python and JSON both are treading in programming fields. Python generates dynamic JSON string and received by the client. To save memory or bandwidth it’s better to send the minified version of JSON text.

Some time for debugging purpose, we need to see beautified JSON data to understand and to Debug. This article will help to pretty print JSON data.

There are two examples in the post.

  1. Pretty Print JSON String
  2. Pretty Print JSON File

1. JSON Pretty Print using Python

Here is an Example:

import json

uglyjson = '{"firstnam":"James","surname":"Bond","mobile":["007-700-007","001-007-007-0007"]}'

#json.load method converts JSON string to Python Object
parsed = json.loads(uglyjson)

print json.dumps(parsed, indent=2, sort_keys=True)

Result :

{
  "firstnam": "James", 
  "mobile": [
    "007-700-007", 
    "001-007-007-0007"
  ], 
  "surname": "Bond"
}

Here is the explanation for the code.

  1. import json

This is a JSON library available in python to convert Python object from JSON string or from JSON file.

  1. json.load

json.load is a method which converts JSON string to Python object. Here is the conversion table form JSON data type to Python data types.

JSON Python
object dict
array list
string unicode
number (int) int,long
number (real) float
true True
false False
null None
  1. json.dumps

json.dums is the opposite of json.load. It creates a JSON string from Python object. It takes parameters indent parameter which formats the JSON string.

2. JSON Print Print from File using Python

Here is the Example:

import json

with open('jsonfile.txt') as jsonfile:
    parsed = json.load(jsonfile)

print json.dumps(parsed, indent=2, sort_keys=True)

This program reads from file and load and create Python objects and dumps the JSON data in beautiful JSON String. Read JSON File using Python for more details

I have used these tools for creating this blog post.

JSON Editor

JSON Viewer

 

Filed Under: JSON, Python Tagged With: json, pretty print, python

Primary Sidebar

Categories

  • Blogging
  • HTML
  • Java
  • JavaScript
  • jQuery
  • JSON
  • MySql
  • Performance
  • PHP
  • Problem Solving
  • Python
  • Testing
  • XML

Copyright © 2021 · Metro Pro on Genesis Framework · WordPress · Log in