# Parsing JSON array with only values

**URL:** https://community.mp3tag.de/t/parsing-json-array-with-only-values/65368
**Category:** Web Sources Discussion
**Created:** [July 24, 2024, 2:17pm UTC](https://community.mp3tag.de/t/parsing-json-array-with-only-values/65368 "2024-07-24T14:17:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![rboss](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rboss](https://community.mp3tag.de/u/rboss)
#### Post date: [July 24, 2024, 2:17pm UTC](https://community.mp3tag.de/t/parsing-json-array-with-only-values/65368/1 "2024-07-24T14:17:01Z")

</div>

Hello everyone,

Here's a simple problem, but one I can't find a solution for:  
From this array

```
{
    "genre": [
        "Hip-Hop/Rap",
        "Music",
        "Hip-Hop",
        "Hardcore Rap"
    ]
}

```

How can I extract all genre values?  
Ideally, the correct command to use would be `json_select_many "array" "object" s s n n`, but since there is no key on each value, what would make sense would be `json_select_many "array" "" s s n n`. But this doesn't work; or rather, returns `[empty]`

I've also tried

```
(...)
json_foreach "genre"
	OutputTo "integer"
	json_foreach_counter
	json_select_array "genre" "%integer%"
	OutputTo "testOutput"
	SayRest
	Set "%integer%"
json_foreach_end

```

This should assign 1~4 to `%integer%` and then use `json_select_array "genre" "1~4" ` to get the values; but this returns an error on `json_select_array "genre" "%integer%"` because `%integer%` is interpreted as a string and not as a number, which breaks the command.

This test data is a valid JSON array, as per validation by [jsonlint.com](http://jsonlint.com).  
So what am I missing?

Thank you in advance.

---

<div class="post-metadata">

### Author: ![rboss](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rboss](https://community.mp3tag.de/u/rboss)
#### Post date: [July 27, 2024, 6:03pm UTC](https://community.mp3tag.de/t/parsing-json-array-with-only-values/65368/2 "2024-07-27T18:03:54Z")

</div>

> [@On another topic](https://community.mp3tag.de/t/65370/1):
>
> In the original `discogs.inc` I found this line  
> `json_select_array "genre" -1 ", "`

Well, this does work..... But if I needed to process each value independently (with an `If` condition, for example), I would need to work with the string generated from that array, and not the array itself.

Using `json_select_array "genre" -1 ","` I would get the following string:

`Hip-Hop/Rap,Music,Hip-Hop,Hardcore Rap`

If I wanted to loop through each value, and apply a condition only if the "Music" genre were present, I could do

```
(...)
RegexpReplace "^.*" ",${0}"
Do
	MoveChar 1
	If "Music"
		SayUntil ","	--> or something else
	EndIf
	FindInLine "," 1 1
	MoveChar -1
While ","

```

which works perfectly fine for "Music", but would cause trouble for "Hip-Hop", because there would be 2 matches. This can be negated by using `If "Hip-Hop,"` instead, because now the `If` condition would match the entire value until the delimiter (for comma-separated values, in this example).

Now, the same process, with the same raw data BUT with key--value pairs:

```auto
{
    "genre": [
        {"key": "Hip-Hop/Rap"},
        {"key": "Music"},
        {"key": "Hip-Hop"},
        {"key": "Hardcore Rap"}
    ]
}

```

To apply a condition if "Hip-Hop" is present:

```
json_foreach "genre"
	json_select "key"
	If "Hip-Hop"
		(...) --> do something
	EndIf
json_foreach_end

```

Which is much simpler.

Other than `json_select_array`, which will -depending on the modifiers- return the nth value of an array, or return all elements from the array in a string -which will then require further processing-, I can't find a way to work with all the elements in a JSON array, _one by one, in a loop_, if said array does not contain keys, _and_ while benefiting from the JSON format of the raw data (that is, enabling the use of JSON related commands).

I hope this time I was able to convey more clearly what I'm hoping to achieve.
