PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 5, 2022

[FIXED] How to get a field on string data in Ruby for Redis?

 September 05, 2022     redis, ruby, unmarshalling     No comments   

Issue

I have a dataset with the name of table1 in Redis like below:

[
  {
    "column-name1": "10.1.10.1",
    "column-name2": "range(100,200)",
    "column-name3": "nam3"
  },
  {
    "column-name1": "2.2.2.2",
    "column-name2": "",
    "column-name3": "range(1024,+inf)"
  },
  {
    "column-name1": "1.1.1.1",
    "column-name2": "",
    "column-name3": "nam3"
  }
]

I want to get values of table1.. How can I do it?

How can I parse table1 in ruby to reach its values?


Solution

Frist, you should parse json data from stored string in Redis. For parsing json in Ruby you can do something like below:

require "redis"
require "json"

red = Redis.new(host: "127.0.0.1", port: 6379, db: 1) <-- connect to redis

table1 = red.get("table1") <-- get table1 value (stringified data)
table1_json = JSON.parse(table1) <-- parse value to get a JSON object

now you have your table1 data as a json object and you can iterate over its elements and get the values you want:

for key in table1_json
    puts(key["column-name1"])
end


Answered By - Mohammadreza Sahelgozin
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing