Filtering values with spaces using JQ

I'm facing an issue where I need to select and filter a value that contains a space in it. My attempt involves using a for loop, but unfortunately, I'm not getting any output with this approach. I even tried utilizing --arg name "$i", however, that didn't yield the desired results either.

The values I am working with are "dummy group 1" and "dummy group 2".

group_list=("dummy group 1", "dummy group 2");

for i in "${group_list[@]//,/}"
do

echo $groupid_json | jq  -r '.result[] | select(.name == "$i") | .groupid'

done

Despite my efforts, nothing seems to be happening. Below is the JSON data that I am attempting to filter:

[
  {
    "groupid": "92",
    "name": "dummy group 1"
  },
  {
    "groupid": "93",
    "name": "dummy group 2"
  }
]

If I directly input the value, then the filtering works as expected.

echo $groupid_json | jq  -r '.result[] | select(.name == "dummy group 1") | .groupid'

Answer №1

To make it function correctly, ensure group_list is declared in the right way.

group_list=("sample group 1" "sample group 2")
for item in "${group_list[@]}"; do
  jq -r --arg name "$item" '.[] | select(.name == $name) .groupid' <<< $groupid_json
done

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble retrieving a value from a .JSON file (likely related to a path issue)

My React component is connected to an API that returns data: class Item extends Component { constructor(props) { super(props); this.state = { output: {} } } componentDidMount() { fetch('http://localhost:3005/products/157963') ...

Utilize fetch API in React to streamline API responses by filtering out specific fields

I have received an API response with various fields, but I only need to extract the description and placeLocation. results: [{placeId: "BHLLC", placeLocation: "BUFR", locationType: "BUFR",…},…] 0: {placeId: "BHLL ...

Utilizing React to dynamically load JSON data and render a component

I am currently facing a challenge in rendering a React component that includes data fetched from a JSON using the fetch() method. Although the API call is successful, I am experiencing difficulties in displaying the retrieved data. Below is the code snip ...

Updating the value of a different key within the same object using React Formik's setFieldValue方法

My objective is to automatically select a value in an option select when the onChange event occurs, and then use setFieldValue to set values for 2 Fields with key-value pairs within the same object. The issue I'm facing: Why does calling setFieldValu ...

What are the benefits of adding member functions to the data structures of React.js store?

Using React.js and Typescript, I store plain Javascript objects in the React.js store. These objects are sometimes received from the server without any member functions, but I wish to add functions for better organization. Instead of having to rely on exte ...

Accessing a specific data point from a Rest Api with React JS

How can I extract the value '00000000000000000000000000000000' that comes after clusters in the links->href in a Rest Api response? [{ "analysisUnits": [ { "links": [ { "href": "http://127.0. ...

Having trouble loading data.json file in React.js and jQuery intergration

Having a background in mobile development, I am relatively new to web development so please excuse any amateur questions. Currently, I am working on a react.js project using create-react-app (which utilizes Babel). I am following a tutorial that requires ...

Parsing JSON in React resulted in an undefined object

It's worth mentioning that I am not a React expert and am simply trying my best to learn. If there are any obvious errors, please do point them out. I have been attempting to convert a JSON file into an object so that I can easily parse its contents ...

Preparing my JSON data for visualization on a chart

I have successfully retrieved data using this API, but now I need to transform it into a chart within my react project. As a newcomer to JS and React, I am struggling to create a chart with the JSON data. My objective is to display prices by bedrooms over ...

Function not executing on button press in React.js

I am trying to trigger a function upon clicking a button, but unfortunately, nothing is happening. Despite adding a console.warn() statement inside the function, it doesn't seem to be logging anything. I've looked through similar Stack Overflow s ...

Utilizing React JS to dynamically populate a table with data from an external JSON file

As a newcomer to the realm of React, I am facing challenges in displaying my JSON data in a table. class GetUnassignedUsers extends React.Component { constructor () { super(); this.state = { data:[] }; } com ...

Tips for maintaining the state in a React class component for the UI while navigating or refreshing the page

Is there a way to persist the selection stored in state even after page navigation? I have heard that using local storage is a possible solution, which is my preferred method. However, I have only found resources for implementing this in functional compone ...

Uncover the secrets to retrieving JSON data using the React fetch method

I've been struggling to retrieve data from an API using various methods, but without success. I have included my code and the API below in hopes of utilizing this JSON data with the React fetch method. // Fetching Data from API fetchData() { ...

Looking to locate or track duplicate values within a multi-dimensional array?

In a multidimensional array, I am looking to detect and count duplicates. If duplicates are found, an alert should be triggered. Arr =[[2,"sk"],[3,"df"],[7,"uz"],[3,"df"],[7,"gh"]] Suggestions: One way to ...

Is there a way to display my array within my React component using JavaScript?

I am working with an element that looks like this: const DropdownElements = [ { key: 1, title: "City", placeholder: "Select City", apiUrl: "https://api.npoint.io/995de746afde6410e3bd", type: "city&qu ...

reactjs error: Attempting to utilize the toLowerCase method on an undefined property during double mapping

My issue involves a JSON structure that requires mapping a function twice in a loop to access objects within an array. Once mapped, I need a textbox for searching the data list. However, I am encountering the following error: TypeError: Cannot read proper ...

Updating a component's property in React from a different component

I am completely new to using React, so please be patient with me. Currently, I am working on a webpage that incorporates React components. However, the entire page cannot be built using React; it's not an option for me. What I am trying to achieve i ...

I am facing an issue with Lotties having a black background in my NextJS project. Is there a way to make them transparent

I am facing an issue with my LottieFiles (JSON) displaying a black background when imported into my NextJS project. Despite trying to set background='transparent' on both the Player and parent div, the problem persists. I made sure to export my ...

Access the contents of objects during the creation process

I am currently in the process of creating a large object that includes API path variables. The challenge I am facing is the need to frequently modify these API paths for application migration purposes. To address this, I had the idea of consolidating base ...

Tips for preserving axois GET response as a JSON object or in its original form upon arrival

export class LoadProfile extends Component { state = { data: '' } componentDidMount() { axios({ url: 'http://localhost:8080/profile/all', method: 'GET', responseType: 'json', ...