-1

I have a json file similar to the following structure.

{
"anakategori": [
{
"name": "Kadın",
"url": "example.com/kadin",
"img": "..."
},
{
"name": "Erkek",
"url": "example.com/erkek",
"img": "..."
},
{
"name": "Ayakkabı &Çanta",
"url": "example.com/ayakkabi-canta",
"img": "..."
},
{
"name": "Anne &Bebek",
"url": "example.com/anne-bebek",
"img": "..."
},
{
"name": "Elektronik",
"url": "example.com/elektronik",
"img": "..."
},
{
"name": "Ev &Yaşam",
"url": "example.com/ev-yasam",
"img": "..."
},
{
"name": "Kozmetik",
"url": "example.com/kozmetik-kisisel-bakim",
"img": "..."
},
{
"name": "Saat &Aksesuar",
"url": "example.com/saat-aksesuar",
"img": "..."
},
{
"name": "Kitap,Müzik,Film,Oyun",
"url": "example.com/kitap-muzik-film-oyun",
"img": "..."
},
{
"name": "Bahçe,Yapı Market,Oto",
"url": "example.com/bahce-yapi-market,-oto",
"img": "..."
},
{
"name": "Çok Satan Ürünler",
"url": "example.com/cok-satan-urunler",
"img": "..."
},
{
"name": "İndirimli Ürünler",
"url": "example.com/indirimli-urunler",
"img": "..."
},
{
"name": "En Çok Görüntülenenler",
"url": "example.com/en-cok-goruntulenenler",
"img": "..."
},
{
"name": "Markalar",
"url": "example.com/markalar",
"img": "..."
}
]
}

In this file, I want to automatically fill the "img": "..." field with the url sorted in the order of the above.

The urls in the file are like this:

example.com/category/kadin.png
example.com/category/erkek.png
example.com/category/ayakkabicanta.png

Thanks in advance for your help.

I got the urls in order with regex from the json file and I got the img urls from the metadata, now how can I add them to this file in order?

Mehmet
  • 1
  • 1
  • I think you will not need `regex`, but it's unclear what tools you like to use to accomplish this. Currently this question might be a duplicate of: [Modify a key-value in a json using jq in-place](https://stackoverflow.com/questions/42716734/modify-a-key-value-in-a-json-using-jq-in-place) – Luuk Dec 03 '22 at 16:33
  • yes, similar issue thanks. Unfortunately, I don't know how and what to use the img urls in the txt file into "img" in order :( maybe you can help me about the topics that I need to research and learn. – Mehmet Dec 03 '22 at 16:49

1 Answers1

0

When you have stored your file in movies.json, you can do this:

$ jq jq '.["anakategori"][0]' movies.json
{
  "name": "Kadın",
  "url": "example.com/kadin",
  "img": "..."
}
$ jq '.["anakategori"][0]["img"]' movies.json
"..."

and to update this value (inplace, using the Q/A Modify a key-value in a json using jq in-place):

$ jq '.["anakategori"][0]["img"] = "test"' movies.json >abc && mv abc movies.json
jq '.["anakategori"][0]' movies.json
{
  "name": "Kadın",
  "url": "example.com/kadin",
  "img": "test"
}

Now you see the url repleace by the text "test".

The 0 in above examples refers to the first occurrence of a movie in your movies.json file.

This should help you get started, the rest of the things needed can be found on Stackoverflow too, like Looping through the content of a file in Bash

Luuk
  • 12,245
  • 5
  • 22
  • 33