×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Convert String to an Array in JSON file using Shell scripting

Convert String to an Array in JSON file using Shell scripting

Convert String to an Array in JSON file using Shell scripting

(OP)
This is the sample json I have pasted here. I want all the IP address strings to be converted into an array. For example "10.38.32.202" has to be converted to ["10.38.32.202"] 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": "10.38.32.202","mac_address": "A0:12:34:45","network_ip_v4_address":["10.38.61.1","10.38.32.1"]}

RE: Convert String to an Array in JSON file using Shell scripting

Look into a Linux utility called "jq". It can translate between json and several other formats easily (including plain text). It can also query data out of a json format file. Google will get you way more information on how to use it than a few replies here.

RE: Convert String to an Array in JSON file using Shell scripting

The example JSON you posted is not valid, because you have duplicate key 'network_ip_v4_address' - e.g. try https://jsonlint.com/

CODE

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


If you would provide valid JSON for example like this

CODE

{ 
  "network_ip_v4_address": "10.38.32.202", 
  "mac_address": "A0:12:34:45"
} 
you can try this jq command, which puts the ip address into the array:

CODE

$ echo '{"network_ip_v4_address": "10.38.32.202", "mac_address": "A0:12:34:45"}' | jq '. | .network_ip_v4_address = (if (.network_ip_v4_address | type) == "string" then [.network_ip_v4_address] else .network_ip_v4_address end)' 
i.e. the output is:

CODE

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

Trying the same script on JSON where the ip addresses are in the array like

CODE

{
  "network_ip_v4_address": ["10.38.61.1", "10.38.32.1"],
  "mac_address": "A0:12:34:45"
} 
lets the array unchanged:

CODE

$ echo '{"network_ip_v4_address": ["10.38.61.1", "10.38.32.1"], "mac_address": "A0:12:34:45"}' | jq '. | .network_ip_v4_address = (if (.network_ip_v4_address | type) == "string" then [.network_ip_v4_address] else .network_ip_v4_address end)' 
i.e. the result is:

CODE

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close