0

i have a details.json file with a lot of entries and a shops.txt file like below. I like to have a little script which compares two values and just return the matching json entries.

[
{
    "userName": "Anne",
    "email": "anne@stack.com",
    "company": {
        "name": "Stack GmbH",
    },
    "details": {
        "key": "EFHJKI-KJEFT-DHMNEB",
        "prod": "Car",
    },
    "store": {
        "id": "05611a7f-a679-12ad-a3u2-0745e3650a03",
        "storeName": "shop-a57ca0a3-120c-1a73-153b-fa4231cab768",
    }
},
{
    "userName": "Tom",
    "email": "tom@stack.com",
    "company": {
        "name": "Stack GmbH",
    },
    "details": {
        "key": "DFSGSE-FGEAR-GWRTGW",
        "prod": "Bike",
    },
    "store": null
},
]

This is the other file "shops.txt" (can be a lot more of shops inside)

shop-a57ca0a3-120c-1a73-153b-fa4231cab768

The script is looping through the shops, for every shop it loops through the json and should compare the currentShop with the store.shop from json and then echo the user and the shop. But I can not access the specific parameters inside the json. How can I do this?

#!/bin/bash
shops="shops.txt"
while IFS= read -r line
do
  currentShop="$line"

  jq -c '.[.userName, .store.storeName]' details.json | while read i; do
  if [[ $i.store.storeName == *$currentShop* ]]; then
  echo $i.userName
  echo $currentShop
  fi
  done

done < "$shops"
midi
  • 3,128
  • 5
  • 30
  • 47
  • I don't understand the logic behind your program, but I haven't worked with `jq` either. I find it strange, that you seem to extract two pieces of data (user name and store name), but have only a single scalar variable (`i`). Did you try to debug your code using `set -x`.. – user1934428 Sep 14 '21 at 11:20
  • i is one of the entries from the json array. But idk how to access the single values of one entry – midi Sep 14 '21 at 11:44
  • But you are not reading an **array**. `read i` reads one word (scalar) from stdin and stores it in `i`. – user1934428 Sep 14 '21 at 11:46

2 Answers2

1

First of all, you might want to 'clean' your json, remove any trailing ,'s etc.


After looping through each line in the file we just need one select() to get the matching object.

The script could look something like:

#!/bin/bash

while read shop; do
  echo "Check: $shop"
  jq -r --arg storeName "$shop" '.[] | select(.store.storeName == "\($storeName)") | "\(.userName) - \(.store.storeName)"' details.json
done < "shops.txt"

Which will produce

Check: shop-a57ca0a3-120c-1a73-153b-fa4231cab768
Anne - shop-a57ca0a3-120c-1a73-153b-fa4231cab768

I guess this could be combined into a single call, but it seems like you want to loop over each entry found


You can test this selector on this online JqPlay Demo.

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Do I need to echo it? Because I don't get any output except of "Check: shop-...." – midi Sep 14 '21 at 12:23
  • Nope, the `echo` is just to show the current iterator. You can remove it. – 0stone0 Sep 14 '21 at 12:24
  • I meant do I need to echo the line with jq? Because I have no output except the first echo. Without the first I have no output at all. – midi Sep 14 '21 at 12:29
  • Then there must be something else going on. Maybe your files are named differently? This should work, – 0stone0 Sep 14 '21 at 12:31
  • 1
    Ok, I will get a coffe and look into it again later. Thank you very much for your help ;) – midi Sep 14 '21 at 12:35
  • Please see my edit. You might want to double check your json, remove any trailing `,`. Also, I've added a JqPLay demo for you to test. – 0stone0 Sep 14 '21 at 12:36
  • I already removed the "," in the json. I will check later. Thx. In the jqPlay it works – midi Sep 14 '21 at 12:49
0

I was able to access the values with the following command:

echo $i | jq -r '.userName'
midi
  • 3,128
  • 5
  • 30
  • 47