UPDATE! The community will be go into read-only on April 19, 8am Pacific in preparation for migration on April 21. Read more.
UPDATE! The community will be go into read-only on April 19, 8am Pacific in preparation for migration on April 21.Read more.
Cadet 1st Class
Cadet 1st Class
300 views

What is best way to convert text/text file to json?

We are using visual cobol 2.3, framework 4.5. I need to send out a json file. We don't currently work with json so I am not sure what I am doing. I seen there is a json generate statement in cobol upgrades, but upgrading is not an option for us right now. I guess I envision this to have a text file that we would run a program over to convert to json.  How would the text file need set up? What statements/libraries would be needed? Thanks!

0 Likes
2 Replies
Micro Focus Expert
Micro Focus Expert

@BeckyG 

As you mentioned Framework 4.5 am I correct in thinking that you are working with a .NET application ? If you are, an option would be to try using Json.NET.

0 Likes
Micro Focus Frequent Contributor
Micro Focus Frequent Contributor

Also

 

Have you considered using jq?

 

To transform a text file into a json file.

 

https://stedolan.github.io/jq/

 

This has a download for windows.

 

This is an example jq script.

<>> texttojson.jq

#
# run this command with input from hostnamectl
# hostnamectl | jq -n -R -f texttojson.jq
# or add jq json query
# hostnamectl | jq -n -R -f texttojson.jq | jq .thiscomputer.hostnamectl["\"CPE OS Name\""]
# "cpe:/o:redhat:enterprise_linux:8.2:GA"
# or using textfile input
# hostnamectl >hostnamectl.txt
# jq -n -R -f texttojson.jq hostnamectl.txt >texttojson.json
# jq .thiscomputer.hostnamectl["\"CPE OS Name\""] texttojson.json
# "cpe:/o:redhat:enterprise_linux:8.2:GA"
# 
# 
#
{"thiscomputer":
    {"hostnamectl": [ inputs
                    | gsub("^\\s+|\\s+$";"")
                    | split(": ")
                    | {(.[0]): .[1]}
                  ]  
      | add
    }
}

 

Turns this text output, from the unix hostnamectl command

<>> 

$ hostnamectl
   Static hostname: tonyt-rhel8-01.microfocus.aws.lab
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 34de6610bf0046b399122b1892d203b1
           Boot ID: 8e606f7c2298409db868254128bdf606
    Virtualization: xen
  Operating System: Red Hat Enterprise Linux 8.2 (Ootpa)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:8.2:GA
            Kernel: Linux 4.18.0-193.el8.x86_64
      Architecture: x86-64
$

 

Into this, using this command

$ hostnamectl | jq -n -R -f texttojson.jq
{
  "thiscomputer": {
    "hostnamectl": {
      "Static hostname": "tonyt-rhel8-01.microfocus.aws.lab",
      "Icon name": "computer-vm",
      "Chassis": "vm",
      "Machine ID": "34de6610bf0046b399122b1892d203b1",
      "Boot ID": "8e606f7c2298409db868254128bdf606",
      "Virtualization": "xen",
      "Operating System": "Red Hat Enterprise Linux 8.2 (Ootpa)",
      "CPE OS Name": "cpe:/o:redhat:enterprise_linux:8.2:GA",
      "Kernel": "Linux 4.18.0-193.el8.x86_64",
      "Architecture": "x86-64"
    }
  }
}
$

 

The thing that does the main work is the split(“: “)

 

You could comment delimit the file output by cobol and use split(“, “)

 

Once you have many json files you can use jq to mangle/merge them together it is very powerful.

 

Further reading

https://thoughtbot.com/blog/jq-is-sed-for-json

 

 

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.