Convert String to an Array using shell scripting in JSON file.

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Convert String to an Array using shell scripting in JSON file.
# 1  
Convert String to an Array using shell scripting in JSON file.

This is the sample json I have pasted here. I want all the IP address strings to be converted into an array. For example "" has to be converted to [""] everywhere in the JSON. There are multiple IPs in a JSON I am pasting one sample object from the JSON. But the IPs already in an Array should not be altered.

{"network_ip_v4_address": "","mac_address": "A0:12:34:45","network_ip_v4_address":["",""]}

Last edited by vbe; 11-27-2019 at 11:24 AM..
# 2  
The clean way to do this is to read the JSON file into an array (process the JSON) and then write back to JSON in the format you desire.

There are many JSON libs to do this in Javascript, PHP, Python and yes, even with shell utilities:

Maybe try jq ?

jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed , awk , grep and friends let you play with text. jq is written in portable C, and it has zero runtime dependencies.

I'm not a jq user, because I process all my JSON in either Javascript, PHP or Python, but there are some folks here who are certainly jq users.
# 3  
Not a big expert on jq but with a bit of playing around I got this to work:

jq 'if .network_ip_v4_address | type == "string" then .network_ip_v4_address |= [ . ] else . end' infile

Last edited by Chubler_XL; 11-27-2019 at 04:07 PM..
# 4  
After reading your request again it appears you want to identify an IP addresses in any non-array JSON data array and convert it to an array.

input :
{"adaptor" : "ip01" , "gateway" : "" , "net_mask" : "" }
{"ip_v4_address": "","mac_address": "A0:12:34:45"}
{"adaptor": "ip12", "network_ip_v4_address":["",""]}

{"adaptor" : "ip01" , "gateway" : [""] , "net_mask" : [""]}
{"ip_v4_address" : [""] , "mac_address" : "A0:12:34:45"}
{"adaptor" : "ip12" , "network_ip_v4_address" : ["",""]}

I have written a decend(f) function that applies f recursively to every component, this is similar to the walk(f) function already in jq, except it doesn't apply f to all the array elements. I then use this to identify any string values which are IP addresses and convert them to an array:

jq -c '
def decend(f):
  . as $in | 
  if type == "object"
      reduce keys[] as $key
      ( {}; . + { ($key) : ($in[$key] | decend(f)) } ) | f
  else f

   if type == "string" and
   then [ . ] 
   else . 
   end)' infile

Last edited by Chubler_XL; 11-27-2019 at 05:59 PM..
These 2 Users Gave Thanks to Chubler_XL For This Post:
# 5  
Not sure why it is not working for me. I am getting the output but without the IP address in String.



Last edited by Chubler_XL; 11-27-2019 at 10:12 PM.. Reason: Added missing code tags
# 6  
It's not valid to have same key multiple times in the one record. Try running your input thru a JSON validator
This User Gave Thanks to Chubler_XL For This Post:
# 7  
Its working thank you so much.

--- Post updated at 02:26 AM ---

How to permanently apply these changes to the file.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #524
Difficulty: Medium
The ASCII table was designed to enforce a lexicographic ordering: letters are in alphabetic order, uppercase precede lowercase versions, and numbers come after both.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to convert any shell command output to JSON format?

Hi All, I am new to shell scripting, Need your help in creating a shell script which converts any unix command output to JSON format output. example: sample df -h command ouput : Filesystem size used avail capacity Mounted /dev/dsk/c1t0d0s0 8.1G 4.0G 4.0G 50% /... (13 Replies)
Discussion started by: balu1234
13 Replies

2. Shell Programming and Scripting

Array size in C shell scripting

Hi, I would like to know how to define the size of the array in c shell scripting. (15 Replies)
Discussion started by: gopishrine
15 Replies

3. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

4. Shell Programming and Scripting

Convert json to xml

Hello everyone, I have created a workflow that will pull down data via a RESTful API in JSON, then my code parses and modifies said data. The problem I have is that these APIs I am working with only accept XML to PUT/POST data, and I am looking for an easy way to convert my JSON file to XML. ... (2 Replies)
Discussion started by: Zaphod_B
2 Replies

5. Shell Programming and Scripting

UNIX or Perl script to convert JSON to CSV

Is there a Unix or Perl script that converts JSON files to CSV or tab delimited format? We are running AIX 6.1. Thanks in advance! (1 Reply)
Discussion started by: warpmail
1 Replies

6. Programming

Perl array / json

Hi All I have used the below code to print the dumper of a json #!/usr/bin/perl use LWP::Simple; use JSON qw( decode_json ); use Data::Dumper; use strict; use warnings; my (%list); my $trendsurl =... (5 Replies)
Discussion started by: ab52
5 Replies

7. Shell Programming and Scripting

How to convert string into integer in shell scripting?

Hi All, sessionid_remote=$(echo "select odb_sessionid from sysopendb where odb_dbname='syscdr';" | sudo -u cucluster ssh ucbu-aricent-vm93 "source /opt/cisco/connection/lib/connection.profile; $INFORMIXDIR/bin/dbaccess sysmaster@ciscounity") for sid in $sessionid_remote;do if * ]];... (2 Replies)
Discussion started by: deeptis
2 Replies

8. Shell Programming and Scripting

array + if in linux shell scripting

Hi, I am having two set of files with different number of columns and rows. A set of files have only single row with 20 columns. B set of files have 1000s of rows with 5 columns. both set contains equal number of files. I want to save all the 20 columns of A in variables one by one and... (21 Replies)
Discussion started by: CAch
21 Replies

9. Shell Programming and Scripting

how to convert array into the string

Hi I have a line/string as follows: A=" 3498 NORDEA - INDX LINKED NORY" which was converted into an array of characters: p321$ echo "${ARR}" 3 4 9 8 N O R D E A - I N D X L I N K E D N O R Y When I am trying print this array there are blank... (4 Replies)
Discussion started by: aoussenko
4 Replies

10. Shell Programming and Scripting

How to Rename/Convert Files in Shell Scripting?

Hi All, I want to Rename/Convert all the .doc files in to .pdf format. I am using the following Script. But the final output is not proper. ########################################## cd /u13/prepaid/ftpdata/INfiles/sap/ for name in `ls *.doc` do name1=`echo $name | sed -e... (11 Replies)
Discussion started by: hanu_oracle
11 Replies

Featured Tech Videos