Extracting data from a Firebase realtime database JSON-export is a straightforward process that can be

I'm currently working with a Firebase realtime database export in JSON format that contains nested information that I need to extract. The JSON structure is as follows:

{
  "users" : {
    "024w97mv8NftGFY8THfQIU6PhaJ3" : {
      "email" : "xxx",
      "id" : "024w97mv8NftGFY8THfQIU6PhaJ3",
      "name" : "xxx",
      "items" : {
        "-LQL9n-r9BGdo3HJZ2sk" : {
          "disliked" : true,
          "id" : 396,
          "name" : "Aaa"
        },
        "-LQL9oO63nH-QW2w6zz0" : {
          "liked" : true,
          "id" : 3674,
          "name" : "Bbb"
        }
      }
    },
    "0ERLT5DLRvbZUnjlnM7Ow0qItpz2" : {
      "email" : "zzz",
      "id" : "0ERLT5DLRvbZUnjlnM7Ow0qItpz2",
      "name" : "zzz",
      "items" : {
        "-LIZnriSVQMzqTsPFNYa" : {
          "id" : 396,
          "liked" : true,
          "name" : "Aaa"
        },
        "-LIZnrzOuk4WyjqEiLG8" : {
          "disliked" : true,
          "id" : 4805,
          "name" : "Ccc"
        }
      }
    }
  }
}


My goal is to compile a list of all liked item names and potentially calculate the frequency at which an item is liked. Can anyone recommend a simple tool or script for accomplishing this task? Preferably in Ruby or Javascript. Thank you!

Answer №1

If you want to parse JSON data in Ruby, you can do it like this:

result_hash = JSON.parse(result)
result_ary = result_hash["users"].collect do |k,v| 
  v["items"].values.select{|v1| v1["liked"] == true } 
end
result_data = result_ary.flatten

The parsed result looks like this:

=> [{"liked"=>true, "id"=>3674, "name"=>"Bbb"}, {"id"=>396, "liked"=>true, "name"=>"Aaa"}]

Now you can easily get the results you need:

result_data.collect{|x| x["name"] }
=> ["Bbb", "Aaa"]

 result_data.count {|x| x["name"] == "Aaa"}
=> 1

result_data.count {|x| x["name"] == "Bbb"}
=> 1

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

Generating innerHTML and retrieving attributes simultaneously from a single Div element

Looking for a way to extract an attribute from a div and use it to create a link within the same div with the attribute included in the src. Currently, my code only picks up the first attribute, resulting in all links being the same. I am still a beginne ...

The use of JSON.parse does not support parsing URL links

As a junior developer specializing in Javascript and Google Apps Script, I decided to enhance the functionality of my Google Sheets by tracking the last modification time of URLs stored in them. Although I attempted to create a script for this task, it see ...

Steps for assigning a 404 status code upon promise rejection

My approach to testing the login functionality involves using promises and chaining them. If the user enters an invalid password, the data is rejected; otherwise, it is resolved. I then verify if the user is logged in successfully by chaining these methods ...

The significance of using the Spread operator in React-Redux Reducers

Exploring the essence of the spread operator has been quite intriguing. Based on my interpretation of the documentation, it seems that the spread syntax essentially duplicates the existing object and then gets replaced by a new object when one is introduce ...

Strategies for effectively and speedily parsing extensive line-formatted JSON files

The scenario: Having a substantial amount of data from a social media platform spanning 5 months, stored as line-formatted json files divided by minutes. Here is more information All the data is hosted on a server with 400 gb of RAM and a 32 core, 64 thr ...

Parameters for MongoDB search queries

I am currently working with a MongoDB query on a specific collection. Here is the structure of the query: common.db.collection('vb.vbStats').find({uid:uid, "vbs.vbNID":vbNID}, {"vbs.$":1}).toArray(function(err, result) {....} The collection I a ...

Mongoose fails to carry out internal query using Promise mechanism

Just diving into the asynchronous and await world, I decided to play around with Mongoose + MongoDB + Node.JS. Here is a snippet of my code: exports.updateBrandPreferences = async (req,res) => { var userID = req.body.playerID; var newBrands = r ...

Why does Javascript Tic-Tac-Toe delay notifying the user until the entire board is full?

There have been a lot of questions about Tic-Tac-Toe javascript, but my issue is unique. My code works fine, but it doesn't declare a winner or a tie game until the entire board is filled. I suspect that my if statements are causing problems and can&a ...

Is there a way to configure cloudantDb.search to fetch all records without the default limitations of 25 records and

I've been exploring ways to optimize search functionality by utilizing search indexes. Here is the code snippet I've been working with: return new Promise((resolve, reject) => { let conf = {'include_docs': true} conf.q = "user: ...

Create a fresh array by merging two existing arrays together

My project involves working with two separate arrays. The first array contains normal date values: var = [ "2022-05-01", "2022-05-02", ... "2022-05-30" ] The second array consists of objects that contain s ...

A discrepancy has arisen as the value within the array structure is being altered

I am facing an issue with the array structure of an object in my Angular front-end code. When I add values to the array based on selection, the object looks like this: todayRates: 10 yesterdayRates: 5 to: Array(1) 0: [3] length: 1 __proto__: Array(0) __pro ...

Is there a way to retrieve the value from a select tag and pass it as a parameter to a JavaScript function?

I would like to pass parameters to a JavaScript function. The function will then display telephone numbers based on the provided parameters. <select> <option value="name-kate">Kate</option> <option value="name-john">John& ...

Utilizing Parallax.js within the React Framework

Just starting out with React and attempting to integrate the Parallax.js library into my project. I've completed the installation using npm, imported the library, and followed this helpful discussion related to my query. However, I'm encounterin ...

Check if a rotated rectangle lies within the circular boundary of the canvas

I have a rectangular shape that has been rotated using the ctx.rotate method, and there is also an arc on the canvas. My goal is to determine if any part of the rectangle lies within the boundaries of the arc. See the example below: https://i.sstatic.net/ ...

Creating a JSON-based notification system in Unity

I am currently working on developing a notification list feature in Unity that will be populated by a JSON API. The goal is to display a series of updates, announcements, and other information items, showcasing both the title and content of each piece of n ...

Problem with Jquery Focus-Out in Internet Explorer on two textboxes

I am working with two text-boxes, each of which has a Focus-Out event attached to it. $('.ClassofTxtbx1').focusout(function(){ }); $('.ClassofTxtbx2').focusout(function(){ }); Strangely, when I input something in textbox1 and press ta ...

Tips for preventing child elements from becoming distorted when the parent element is resized

How can I prevent rotated child elements from skewing when the parent element is scaled? Here is the code snippet that I am working with: <svg viewbox="0 0 500 500"> <g class="parent" transform="matrix(1.71341 0 0 1 -42.302 0)"> <path ...

tag directive not functioning properly

I have encountered an issue while trying to create a simple custom directive. When I specify the directive as <div my-info></div> in my HTML file, it works fine. However, when I specify it as <my-info></my-info>, it does not work. ...

Chokidar operates smoothly from the command line, but fails to function properly when invoked through the use of 'npm run'

I've implemented a script that monitors Tailwind CSS files using Chokidar. The script works perfectly when I execute the command in the CLI using chokidar 'tailwind.config.js' --initial=true -c 'npm run build-tailwind'. It successf ...

How can I link the function name stored in JSON data to the (click) event of a button in an Angular project?

Seeking assistance with assigning method names dynamically and utilizing them in my code. Here is what I have attempted so far: <div class="dashboard row"> <div class="card col-lg-3" *ngFor="let card of cards"> <h5 class="card-title"> ...