2

I am trying to convert map of objects to an array with jq

My Input is

{
  "fish-chips": {
    "likeDislikeRatio": ["80%", "20%"],
    "country": ["BRIT_FOOD","USA_FOOD"]
  },
  "sausage": {
    "likeDislikeRatio": ["75%", "25%"],
    "country": ["EU_FOOD"]
  },
  "cheese-burger": {
    "likeDislikeRatio": ["25%", "75%"],
    "country": [
      "BRIT_FOOD",
      "USA_FOOD",
      "EU_FOOD",
      "DEFAULT"
    ]
  }
}

And my expected output is

[
  {
"name" : "fish-chips",    
"likeDislikeRatio": [
      "80%",
      "20%"
    ],
    "country": [
      "BRIT_FOOD",
      "USA_FOOD"
    ]
  }
]
[
  {
"name" : "sausage",
    "likeDislikeRatio": [
      "75%",
      "25%"
    ],
    "country": [
      "EU_FOOD"
    ]
  }
]
[
  {
"name" : "cheese-burger",
    "likeDislikeRatio": [
      "25%",
      "75%"
    ],
    "country": [
      "BRIT_FOOD",
      "USA_FOOD",
      "EU_FOOD",
      "DEFAULT"
    ]
  }
]

what I tried The closest filter I could find was to_entries[] | [.key, .value]

but the output is not as what I expected and I feel that there should be one more filter with this to get the desired out put

Current Output

[
  "fish-chips",
  {
    "likeDislikeRatio": [
      "80%",
      "20%"
    ],
    "country": [
      "BRIT_FOOD",
      "USA_FOOD"
    ]
  }
]
[
  "sausage",
  {
    "likeDislikeRatio": [
      "75%",
      "25%"
    ],
    "country": [
      "EU_FOOD"
    ]
  }
]
[
  "cheese-burger",
  {
    "likeDislikeRatio": [
      "25%",
      "75%"
    ],
    "country": [
      "BRIT_FOOD",
      "USA_FOOD",
      "EU_FOOD",
      "DEFAULT"
    ]
  }
]

I saw many posts on doing the opposite that is converting array to map using either reduce or INDEX but not what I expected.

1 Answer 1

3

You were close.

to_entries[] | [{name: .key} + .value]

Online demo

Sign up to request clarification or add additional context in comments.

6 Comments

I am not sure if the followup question can be asked. Is it possible to further have a filter and output has added text and filters like this
split_clients "${remote_addr}${http_user_agent}" $fish_chips_default { 80% "fish-chips=0"; 20% "fish-chips=1"; } split_clients "${remote_addr}${http_user_agent}" $sausage_default { 25% "sausage=0"; 75% "sausage=1"; }
map $foodExpPage $foodexperiments { default $cheese_burger; "brit" '$cheese_burger,$fish_chips'; "usa" $cheese_burger; "eu" '$sausage'; }
As you can see it has lots of character addition and replacement. Is this possible with further filters?
@SandeepNair I think you should post another question for that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.