Ansible read JSON file - JSON file Parsing | DevOps Junction (2023)

In this article, we are going to see how to read the JSON file in ansible-playbook

JSON is the most famous data representation it is widely used to store and transmit data. When you are working in Ansible automation, you might get a requirement to work with JSON files and read JSON files into Ansible playbook and process the data and store the data as a variables.

In general, JSON data can be processed easily with json_query module of ansible and you can read more about it in our previous article

Our primary objective of this article is to talk about how to process the JSON FILE and read the JSON file into ansible playbook and process it.

in the previous article, we have talked about how to directly use the JSON output of a website or URL and use it for further processing. but now we are going to use the file as our source of data in this article.

we are going to take some sample JSON file which contains elements and sub elements in both array and single line formats, to understand this better.

Sample JSON file for our testing

This is the JSON file we are going to be using this article. the file name is config.jsonit was ideally designed for weblogic ansible installation setup but this can serve as a viable example in here.

{ "domain":{ "name": "mwidomain", "prod_mode": "prod", "user": "weblogic", "password": "weblogic1", "admin": { "servername": "AdminServer", "listenport": "8001" }, "machines": [ { "refname": "Machine1", "name": "MWINODE01" }, { "refname": "Machine2", "name": "MWINODE02" } ], "clusters": [ { "refname": "Cluster1", "name": "App1Cluster", "machine": "Box1" }, { "refname": "Cluster2", "name": "App2Cluster", "machine": "Box2" } ], "servers": [ { "name": "ms1", "port": 9001, "machine": "Box1", "clusterrefname": "Cluster1" }, { "name": "ms2", "port": 9002, "machine": "Box2", "clusterrefname": "Cluster2" }, { "name": "ms3", "port": 9003, "machine": "Box1", "clusterrefname": "Cluster1" }, { "name": "ms4", "port": 9004, "machine": "Box2", "clusterrefname": "Cluster2" } ] }}

How to read JSON file into ansible playbook

To read JSON file into the ansible playbook, ansible has given us a filter named from_json

but, of course, we need to use it along with few more commands and module like shell to read the json file.

(Video) Ansible Blockinfile Module

here is the quick ansible playbook example that reads JSON file

--- - name: ReadJsonfile hosts: localhost tasks: - name: Display the JSON file content shell: cat config.json register: result - name: save the Json data to a Variable as a Fact set_fact: jsondata: "{{ result.stdout | from_json }}"

Let us see how this playbook works.

  • The First task or play is to display the json file using the normal Shell command catat this same tasks we are saving this output into a ansible register variable named result
  • The Second task or play is to read the register variable of the previous task named resultand convert that into a properjsondata, this is done using from_jsonfilter and then we store it into a new ansible fact variable named jsondata
  • Now we have our JSON file content stored in jsondatavariable after being formatted as proper JSON, which can be easily parsed now using ansible json query module.

As simple as that we have now learnt how to read JSON data into ansible playbook using from_jsonand shell module.

How to use the JSON data in your playbook as a variable

So now we have processed the JSON file and imported it into our playbook but here comes the next part of the equation. So how are we going to use this JSON data in our playbook or store it as a variable?

Refer the following ansible playbook, A continuation of our previous read json playbook, in this ansible playbook we are taking out the necessary elements/keys and using in our playbook by setting it as ansible fact.

once the values are stored as ansible fact. it can be referred from anywhere in the playbook or ansible role using jinja 2 syntax {{ }}

--- - name: ReadJsonfile hosts: localhost tasks: - name: Display the JSON file content shell: cat config.json register: result - name: save the Json data to a Variable as a Fact set_fact: jsondata: "{{ result.stdout | from_json }}" - name: setDomainName set_fact: domain_name: "{{ jsondata | json_query(jmesquery) }}" vars: jmesquery: 'domain.name' - name: setDomainUsername set_fact: domain_username: "{{ jsondata | json_query(jmesquery) }}" vars: jmesquery: 'domain.user' - name: setDomainPassword set_fact: domain_password: "{{ jsondata | json_query(jmesquery) }}" vars: jmesquery: 'domain.password' - name: setadmin_Listenport set_fact: admin_ListenPort: "{{ jsondata | json_query(jmesquery) }}" vars: jmesquery: 'domain.admin.listenport' - name: Debug the values debug: msg=" Admin Listen Port => {{ admin_ListenPort }}, DomainName => {{ domain_name }}, DomainUserName => {{ domain_username }} , Domain Password => {{ domain_password }}"

Explanation of this playbook.

  • Other than the previously explained two tasks/plays of shelland from_json we have more tasks added with set_factin this playbook
  • set_factis used to create a new ansible variable at a run time of the playbook. we can also call it as run time variables or dynamic variables.
  • To filter out the required key or element from an otherwise big JSON data set. we are here using ansiblejmes_querywhich has been covered in detail here
  • vars is used to set a limited variable for every task/play. it was primarily used to create a variable named jmesquerywhich contains the actual jmesquery
  • The Last play or task in this playbook is to display the created facts/variables using set_fact.

Execution result of this playbook

Here is the output of the playbook.

Complex JSON JMES Queries and Ansible Playbook

Sometimes we would want to create a list from the JSON data like a Dictionary or array. Or let’s say we want to find all the server names and their ports from the JSON we have taken for example.

here is the playbook thatcreates a dictionary of data by processing the JSON file.

(Video) 8. Jinja 2 Template for Ansible

It simply creates a list or dictionary of servers and their port numbers.

--- - name: ReadJsonfile hosts: localhost tasks: - name: Display the JSON file content shell: cat config.json register: result  - name: save the Json data to a Variable as a Fact set_fact:  jsondata: "{{ result.stdout | from_json }}" - name: Server Names set_fact:  servernames: "{{ jsondata | json_query(jmesquery) }}" vars: jmesquery: '*.servers[*].name'  - name: Server Names and Ports set_fact:  serverinfo: "{{ jsondata | json_query(jmesquery) }}" vars: jmesquery: '*.servers[*].[name, port]'  - name: Print all server names debug:  msg: "{{ item}}" with_items:  - "{{ servernames }}" - name: Print all server names debug:  msg: "{{ item }}" with_items:  - "{{ serverinfo }}"

Explanation

  • With the help of jmespathqueries, we are filtering out the Server names and Server Names with Port number like the same way we did in the previous playbook examples.
  • Now we are printing all the server names using the loop iteration with_items

Result of the playbook



Conclusion

I hope this article was helpful to understand how to read and process JSON file with Ansible. Feel free to ask in comments if you have any questions.

Cheers
Sarav

Ansible read JSON file - JSON file Parsing | DevOps Junction (3)

Follow us onFacebook orTwitterFor more practical videos and tutorials. Subscribe to our channelFollow me on Linkedin My ProfileFor any Consultation or to hire us [emailprotected]If you like this article. Show your Support! Buy me a Coffee.

Signup for Exclusive "Subscriber-only" Content

(Video) Devops telephonic interview for experienced | 3+

More from Middleware Inventory

  • Ansible JSON - Parse JSON using Ansible json_query

    Ansible json_query is an on-demand feature that every ansible user wants to explore. In this post we are going to how Ansible JSON processing works. During the infrastructure automation, we might end up in a situation where we need to process Huge datasets, especially in JSON format. We might…

  • ansible search for string in file or check if string exists in file

    The Objective of this post is to show how to search for a string in a file with ansible. ansible provides various ways to accomplish the same. We will cover, three major ways to search for a string in a file. Lineinfile module Using the Shell module and grep command…

  • (Video) AustinTechVideos Live Stream

    ansible update /etc/hosts file with IP of all hosts across all hosts

    In Ansible, SSH Communication is everything. Let it be connecting to remote servers from your controller laptop/desktop (mac/windows) or to enable connectivity between servers on the host group. Especially this will be a huge task to accomplish when you do not have a DNS where each remote host can easily…

  • Ansible Unarchive Module Examples

    What is Ansible Unarchive Module and How does it work Ansible Unarchive module helps to unpack or uncompress the files from an archive file such as tar, tar.gz, zip . Ansible unarchive module can optionally copy the files to the remote server before uncompressing them. Though the unarchive module is…

  • Ansible get_url Examples - How to download file from URL

    (Video) Demystifying The (Departmental) Cloud

    In this post, we are going to see how to download a file from URL using ansible get_url module in other words ansible curl. As we know it already, Ansible has a lot of built-in modules to accomplish all the tasks we might need for configuration management and automation. Ansible…

FAQs

How to parse JSON data in Ansible? ›

To select a single element or a data subset from a complex data structure in JSON format (for example, Ansible facts), use the json_query filter. The json_query filter lets you query a complex JSON structure and iterate over it using a loop structure.

Can Ansible read JSON? ›

Ansible reads a JSON file into a variable

from_json filter - converts the variable to JSON.

How do I parse a JSON file? ›

If you need to parse a JSON string that returns a dictionary, then you can use the json. loads() method. If you need to parse a JSON file that returns a dictionary, then you can use the json. load() method.

How to query a JSON object? ›

To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more information, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).

How do I read and parse JSON files? ›

You can use json.load() method to read a file containing JSON object. Suppose, you have a file named person.json which contains a JSON object. Here, we have used the open() function to read the json file. Then, the file is parsed using json.load() method which gives us a dictionary named data .

How do I parse a response in JSON? ›

Example - Parsing JSON

Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

How do I know if JSON parse? ›

We can use the JSON. parse method by wrapping it inside a try catch block to check if the string is properly parsed or not. If it throws error while parsing that means it is not a valid JSON.

Which method is used to read data from JSON? ›

The fetch API is the preferable method to use when we want to read a JSON file either from an external server or local file into our JavaScript file.

Can I use JSON parse? ›

JSON. parse() is a crucial method for converting JSON data in string form into Javascript objects. It is possible to convert simple or complex objects, but you should never convert calculations or code, like for loops.

What is the easiest way to read a JSON file? ›

How To Open A JSON File On Windows, Mac, Linux & Android
  1. #1) File Viewer Plus.
  2. #2) Altova XMLSpy.
  3. #3) Microsoft Notepad.
  4. #4) Microsoft WordPad.
  5. #5) Notepad++
  6. #6) Mozilla Firefox.
Feb 21, 2023

What does JSON parse () do? ›

The JSON.parse() static method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

Is JSON difficult to parse? ›

JSON is a data interchange format that is easy to parse and generate. JSON is an extension of the syntax used to describe object data in JavaScript.

How to get a value from a JSON? ›

A JSONObject has few important methods to display the values of different types like getString() method to get the string associated with a key string, getInt() method to get the int value associated with a key, getDouble() method to get the double value associated with a key and getBoolean() method to get the boolean ...

How to get data from JSON data? ›

Any JSON data can be consumed from different sources like a local JSON file by fetching the data using an API call. After getting a response from the server, you need to render its value. You can use local JSON files to do an app config, such as API URL management based on a server environment like dev, QA, or prod.

How do I iterate through a JSON object? ›

Use Object.

values() or Object. entries(). These will return an array which we can then iterate over. Note that the const [key, value] = entry; syntax is an example of array destructuring that was introduced to the language in ES2015.

What is the difference between JSON () and JSON parse ()? ›

The difference is: json() is asynchronous and returns a Promise object that resolves to a JavaScript object. JSON. parse() is synchronous can parse a string to (a) JavaScript object(s).

How do I convert a JSON file to readable? ›

How do I convert a JSON file to readable?
  1. Open JSON to String tool and Copy and Paste JSON Code in Input Text Editor.
  2. If you do have a JSON data file, you can upload the file using the Upload file button. ...
  3. Click on JSON to String button once data is available in Input Text Editor, via Paste, File, or URL.

How will you parse the JSON response and validate? ›

To parse the JSON into a JavaScript object, use the standard JSON. parse function. The important thing about validation functions is that they're expected to return a boolean value: true if the response is valid, and false if it's invalid.

How to parse a JSON response and get a particular field from the response? ›

We can parse a JSON response and get a particular field from Response in Rest Assured. This is done with the help of the JSONPath class. To parse a JSON response, we have to first convert the response into a string. To obtain the response we need to use the methods - Response.

What does JSON parse () return? ›

The JSON. parse() method parses a string and returns a JavaScript object. The string has to be written in JSON format.

How long does it take to parse JSON? ›

So you should expect to spend 2 or 3 seconds parsing one gigabyte of JSON data.

What causes JSON parse error? ›

JSON. parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered.

Is JSON parsed automatically? ›

The json auto operator automatically detects where the JSON object is located and parses it.

Does JSON parse have a limit? ›

The Limitations of Parse JSON

Power Automate has a usage limit of 5,000 API requests. Reading the licensing information clarifies that this doesn't mean you can run the flow 5,000 times because the software system considers every flow action as an API request.

Is there a limit to JSON parse? ›

The maximum length of JSON strings. The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data.

How efficient is JSON parse? ›

Parsing JSON is much more efficient than parsing object literals in JavaScript. This is true across all major JavaScript execution engines by up to 2x for an 8MB file, as demonstrated by this parsing benchmark.

How do I read a JSON file with multiple records? ›

To Load and parse a JSON file with multiple JSON objects we need to follow below steps:
  1. Create an empty list called jsonList.
  2. Read the file line by line because each line contains valid JSON. ...
  3. Convert each JSON object into Python dict using a json. ...
  4. Save this dictionary into a list called result jsonList.
May 14, 2021

How do I open a JSON file in read mode? ›

How to Open and Read JSON Files in Python?
  1. Open the json file using the open() function.
  2. Use the json.load() function and pass the file object.
  3. Proceed by using the results of json. load() as a normal python dictionary, and print the contents!
Sep 26, 2021

Is it easier to parse JSON or XML? ›

XML is much more difficult to parse than JSON. JSON is parsed into a ready-to-use JavaScript object.

How can I speed up JSON parsing? ›

Faster, more memory-efficient Python JSON parsing with msgspec
  1. Make sure you don't use too much memory, so you don't crash half-way through.
  2. Parse it as quickly as possible.
  3. Ideally, make sure the data is actually valid up-front, with the right structure, so you don't blow up half-way through your analysis.
May 12, 2022

What is the fastest JSON parser? ›

The simdjson library uses commonly available SIMD instructions and microparallel algorithms to parse JSON 4x faster than RapidJSON and 25x faster than JSON for Modern C++.

What is the weakness of JSON? ›

Security - JSON can be dangerous if used with untrusted browsers or services. JSON service returns a JSON response, which the browser uses directly, and if the browser is not secure, it can be hacked. Thus, it makes web services vulnerable to different kinds of cyberattacks.

How do you check if a key exists in JSON object and get its value? ›

JsonObject::containsKey() returns a bool that tells whether the key was found or not:
  1. true if the key is present in the object.
  2. false if the key is absent of the object.

Can JSON have value without key? ›

JSON doesn't have to have only key:value pairs; the specification allows to any value to be passed without a key.

Can JSON have only value? ›

JSON data, at its simplest, can be a single object composed of fields, with each field having a simple value. Some terminology: A value can be a simple value, an array, or an object. A simple value can be a string, a number, or a Boolean value true or false , or null .

How do you access a JSON object within a JSON object? ›

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.

How to parse JSON into array? ›

Convert JSON to Array Using `json.

The parse() function takes the argument of the JSON source and converts it to the JSON format, because most of the time when you fetch the data from the server the format of the response is the string. Make sure that it has a string value coming from a server or the local source.

How do I iterate over a JSON array of objects? ›

1) Create a Maven project and add json dependency in POM. xml file. 2) Create a string of JSON data which we convert into JSON object to manipulate its data. 3) After that, we get the JSON Array from the JSON Object using getJSONArray() method and store it into a variable of type JSONArray.

How get fetch data from JSON? ›

To fetch JSON from the server using the Fetch API, you need to use the JavaScript fetch() method and then call the response. json() method to get the JSON data as a JavaScript object. The response. json() method reads the data returned by the server and returns a Promise that resolves with a JSON object.

How can I access specific data from JSON? ›

Getting a specific property from a JSON response object

Instead, you select the exact property you want and pull that out through dot notation. The dot ( . ) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you access the values you want from the JSON object.

Can Ansible use JSON format? ›

By default, an Ansible inventory file uses the INI configuration format. You can also use JSON (JavaScript Object Notation) configuration format for Ansible inventory files as well.

Can a Yaml parser parse JSON? ›

You can parse a json with yaml parser. You can also mix json and yaml in the same document and use [..., ..] for annotating arrays and { "bar" : "foo"} for objects. Some yaml libraries allow you to generate yaml that has one style for top level and json style for nested elements / long arrays to improve readability.

What is the correct way to get data using fetch () method? ›

Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.

How do I get fetch specific data? ›

Approach: First make the necessary JavaScript file, HTML file and CSS file. Then store the API URL in a variable (here api_url). Define a async function (here getapi()) and pass api_url in that function. Define a constant response and store the fetched data by await fetch() method.

What is JSON () in fetch? ›

The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON .

How to get values from a JSON object? ›

A JSONObject has few important methods to display the values of different types like getString() method to get the string associated with a key string, getInt() method to get the int value associated with a key, getDouble() method to get the double value associated with a key and getBoolean() method to get the boolean ...

How do I read multiple JSON objects from a JSON file? ›

To Load and parse a JSON file with multiple JSON objects we need to follow below steps:
  1. Create an empty list called jsonList.
  2. Read the file line by line because each line contains valid JSON. ...
  3. Convert each JSON object into Python dict using a json. ...
  4. Save this dictionary into a list called result jsonList.
May 14, 2021

Can JSON parse? ›

parse() The JSON. parse() static method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

What is not allowed in JSON? ›

The following characters must be escaped in JSON data to avoid any problems: " (double quote) \ (backslash) all control characters like \n , \t.

Videos

1. AWS AMIs Amazon Machine Images
(BCReddy Devops)
2. Docker in production, look no hands - Use Case Track
(Docker)
3. Dev versus Ops by Dan Hardiker & Henry Whincup
(Devoxx UK)
4. Ansible for Drupal Infrastructure - DrupalCon LA BOF
(Jeff Geerling)
5. RX M Microservice Architecture
(ExitCertified)
6. 5. Volume create using ONTAP API
(Storage Automation Campus)

References

Top Articles
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated: 06/19/2023

Views: 6125

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.