Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

json - Use jq to turn x=y pairs into key/value pairs

I'm trying to parse environment variables from the JSON output of docker inspect. Annoyingly, those environment variables aren't returned as useful key-value pairs. They're just an array of x=y strings. Here's a relevant snippet of the output:

[
    {
        "Config": {
            "Env": [
                "JENKINS_HOST=1.2.3.4",
                "JENKINS_INSTANCE=tea",
                "JENKINS_NAME=Enterprise Architecture Tools",
                "JENKINS_VERSION=2.46.2",
                "JENKINS_PROTOCOL=http"
            ]
        }
    }
]

I would like to convert that array into something like this:

{
  "Config": {
    "Env": {
      "JENKINS_HOST": "1.2.3.4",
      "JENKINS_INSTANCE": "tea",
      "JENKINS_NAME": "Enterprise Architecture Tools",
      "JENKINS_VERSION": "2.46.2",
      "JENKINS_PROTOCOL": "http"
    }
  }
}

That way, I can use a command like jq '.[] | .Config.Env.JENKINS_HOST' to get the values that I care about. I can't figure out how to accomplish this.

It's relatively easy to select the data and even split the key and value into separate elements. For instance, if I use jq '.[] | .Config.Env | .[] | split("=")', I get data like this:

[
  "JENKINS_HOST",
  "1.2.3.4"
]
[
  "JENKINS_INSTANCE",
  "tea"
]
[
  "JENKINS_NAME",
  "Enterprise Architecture Tools"
]
[
  "JENKINS_VERSION",
  "2.46.2"
]
[
  "JENKINS_PROTOCOL",
  "http"
]

However, I can't figure out how to turn that data into an object assignment. It seems like it should probably be some combination of map or reduce, but I'm stumped. Can anyone point me in the right direction?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To convert an array of two strings (e.g. ["k", "v"]) to an object, you can write:

{ (.[0]) : .[1] }

So you'll want to write something like:

 map(.Config.Env |= (map( split("=") | { (.[0]) : .[1] } ) | add))

a2o

Abstracting out the array-to-object functionality makes the solution a bit more digestible:

def a2o: map( split("=") | { (.[0]) : .[1] } ) | add;

map(.Config.Env |= a2o)

Using match or capture instead of split

Since it is possible for an "=" character to appear in the "value" part of each var=value string, using split naively might not be such a great idea. Here is a more robust alternative, assuming your jq supports regular expressions:

match("([^=]*)=(.*)") | .captures | {(.[0].string) : .[1].string}

Or, slightly more succinctly and perhaps elegantly:

[capture( "(?<key>[^:]*):(?<value>.*)" )] | from_entries

index/1

If your jq does not have regex support, you could use index/1, along these lines:

index("=") as $ix | {(.[:$ix]) : .[$ix+1:]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...