Number of times a value appears in a object

Hi, hope this find you well. I have an object like the one in the image bellow, and I would like to know the number of times a value appear in a certain key. So, In the key "Type" I would like to have an output like "Lemon : 4 times" or if it's with the key "Price" it should be like "12: 3 times, 15:1 time".

Any help will be appreciate it. Thank you.

Welcome to the forum!

I was waiting for someone to provide a Javascript function node example, that can iterate over all of the fields and summarize by value... however, since that has not happened, here is the way I would do it using a change node with a JSONata expression (which is a rather advanced lambda syntax, but ideal for just this situation).

If you know which fields you want to aggregate, it can be as simple-looking as this:

payload.* {
    "FARMER": { FARMER: $count(FARMER) },
    "TYPE": { TYPE: $count(TYPE) },
    "PRICE": { $string(PRICE): $count(PRICE) }
}

which is human-readable terms means: for each object in the payload fields, count each field by value"

Since you did not include any actual data in your post, I did not spend the time to type it in so I could actually test it out -- but that should be close to correct. Just make sure to choose the J: expression option in the pulldown for the change node.

If you get stuck, post some sample data so we can try to help you out some more...

1 Like

Thanks for the explanation, I really appreciate your reply.

I'm taking this data from Firebase using a http request node. I read the data in an object format but also I can convert it to json using the json node. My data looks like that:

{"-MqIOHLFfa8h4qrt4olG":{"FARMER":"EMILIO FELIZ","PRICE":12,"TYPE":"LEMON"},"-MqKOD_jSNKhsxgeWswx":{"FARMER":"EMILIO FELIZ","PRICE":12,"TYPE":"LEMON"},"-MqLVRi5ppVR0bAbg05W":{"FARMER":"MISAEL DE LOS SANTOS","PRICE":13,"TYPE":"LEMON"},"-MqLVYxo2tAKeBNlchbN":{"FARMER":"MISAEL DE LOS SANTOS","PRICE":13,"TYPE":"APPLE"}}

Again, thanks for the reply.

So did you try using that expression inside a change node?

Here's what it looks like in my editor:

It works perfectly. Thank you so much.