bigeast
bigeast

Reputation: 637

update fields with inner values

I have a json string like:

    {
        "name" : "test",
        "properties": {
            "p2": {"test": "test", "value": "b"},
            "p1": {"value": "a"},
            "p3": {"value": "c"}
        }
    }

and I want:

{
"p2" : "b",
"p1" : "a",
"p3" : "c"
}

and the order doesn't matter, but the relation of keys and values must be the same as origin.

I tried jq '.properties | (keys_unsorted, .[].value)' and it gives:

[
  "p2",
  "p1",
  "p3"
]
"b"
"a"
"c"

then I split the command into jq '.properties | keys_unsorted' and jq '.properties | .[].value', do some editing by hands, and use paste command to join the results.

Does a more reasonable way exist?

Upvotes: 0

Views: 61

Answers (1)

oguz ismail
oguz ismail

Reputation: 50750

You're looking for map_values.

$ jq '.properties | map_values(.value)' file
{
  "p2": "b",
  "p1": "a",
  "p3": "c"
}

Upvotes: 2

Related Questions