'Extracting Specific Lines from text file based on content

I'm working on a multi-step process where I run one python script that makes an API call to a system. I then have to extract some of those values from that output so that I can submit them, along with a new password, via a second API call that I would make after.

I have two main questions, first, is it possible to pipe the output of the initial API call directly to a text file rather than just to the console?

Second, is it even possible for python to look through a text file and extract certain lines based on the values in that line?

Some sample output from the first API call:

{
  "LINE_1": true,
  "LINE_2": "Post \"https://a:443/sdk\": dial tcp: lookup a on 1.1.127.252:53: no such host",
  "EXAMPLE_3": "",
  "created_at": 1648040001,
  "deleted": false,
  "deleted_at": 0,
  "delta_interval": 60,
  "description": "API's Testing.",
  "disable_backup": false,
  "hosts_list": [
    {
      "host_name": "TestingHost",
      "port_number": 443
    }
  ],
  "id": "623b1841755f021ebe269xyz",
  "name": "Tester",
  "password": "",
  "scope_id": "5f7e2a15497d4f48a9605687",
  "type": "vcenter",
}

There are four specific lines I am trying to extract: "name": "type": "hosts_list": "username":

I've come across some things describing how to convert lines of a text file into individual objects, but that only allows me to print lines based on the number line they are in a text file, it doesn't print lines based on the content in their lines.

Also, if this is possible, the "hosts_list" line may cause an issue because it actually has a subsection that can contain multiple entries. In the above example the subsection is:

"hosts_list": [
    {
      "host_name": "TestingHost",
      "port_number": 443
    }


Solution 1:[1]

As for your first question, yes, you can:

import json

resp = requests.get('yourURLhere')

with open('a_certain_filename.json','w', encoding='utf-8') as fd:
    json.dump(resp, fd, ensure_ascii=False)

As @MichealButscher commented, the result from your API is a JSON (JavaScript Object Notation). Python's module json provides simple ways to serialize and deserialize data in the form of python dictionaries. For example:

import json

# read the content of the file and store it as string
# NOTE: i'm assuming that both your scripts are in the same folder,
# so they both see your file. If not, update the filename accordingly with the correct path
with open('a_certain_filename.txt', 'r') as fd:
    resp_from_file = fd.read()

# deserialize json to dictionary
deserialized_resp = json.loads(resp_from_file)

then you can access the fields like you would with a classic dictionary:

print(deserialized_resp["name"], deserialized_resp["type"], deserialized_resp["hosts_list"], deserialized_resp["username"])

>>> "Tester", "vcenter", [{"host_name": "TestingHost", "port_number": 443}], "someuser"

As you noted, hosts_list is a nested json object. The module can parse them no problem, they just get parsed, at least in your scenario, as a list of dictionaries. For example:

print(parsed_resp["hosts_list"]

>>> [{"host_name": "TestingHost", "port_number": 443}]

which is an array of 1 element. So, to get, for example, the value "host_name", you would access it like this:

print(parsed_resp["hosts_list"][0]["host_name"])

>>> "TestingHost"

In your scenario, this module would come in handy in the second script while you parse the file with the stored response from the previous call, so that you don't need to manually check by hand strings which is never comfortable.

Documentation:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1