1

The redis INFO commad returns the string like redis_version:2.2.14\r\nredis_git_sha1:00000000\r\nredis_git_dirty:0\r\narch_bits:32

How can I convert the string to get a JSON object something like

 { 
     "redis_version":"x",
     "key2":"value"
 }

4 Answers 4

7

I don't know why would you want to do that, but here's a simple example:

function parseInfo( info ) {
    var lines = info.split( "\r\n" );
    var obj = { };
    for ( var i = 0, l = info.length; i < l; i++ ) {
        var line = lines[ i ];
        if ( line && line.split ) {
            line = line.split( ":" );
            if ( line.length > 1 ) {
                var key = line.shift( );
                obj[ key ] = line.join( ":" );
            }
        }
    }
    return obj;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply... but is there any simple solution instead of the workaround like above?
It is a simple solution and it is not a workaround. What did you expect? That a string will magically parse itself?
I think even redis authors do encourage people to parse it on their own, since it's a relatively simple string to parse.
3

You can use node-redis-info:

npm install redis-info

Usage:

> var parser = require('redis-info');
undefined
> var info = parser.parse(redis_info_str);
undefined
> info.fields.redis_version
2.6.1
> info.startWith('pubsub')
[ [ 'pubsub_channels', '2' ],
  [ 'pubsub_patterns', '0' ] ]
> info.contains('memory')
[ [ 'used_memory', '15080416' ],
  [ 'used_memory_human', '14.38M' ],
  [ 'used_memory_rss', '21258240' ],
  [ 'used_memory_peak', '18985904' ],
  [ 'used_memory_peak_human', '18.11M' ] ]

Comments

1

Simple:

#!/usr/bin/env python
import redis
import json
print(json.dumps(redis.Redis().info()))

For zabbix Template DB Redis:

#!/usr/bin/env python
import redis
import json
import sys
r = redis.Redis.from_url(sys.argv[1])
a = {}
sections = ["CPU","Clients","Cluster","Keyspace","Memory","Modules","Persistence","Replication","Server","Stats"]
for section in sections:
    a[section]=r.info(section)
print(json.dumps(a))

2 Comments

It's asked to use NodeJS, not python.
I'm sorry, but let it stay here, it might be useful to someone else
0
interface RedisInfo {
  [key: string]: any;
}

/**
 * This was taken from how `.serverInfo` was parsed in the ioredis library
 * https://github.com/luin/ioredis/blob/f275bc24de3825f80415a69ff227a45251dd1a3b/lib/redis/index.ts#L500
 * @param infoString Result of Redis INFO command
 */
const parseRedisInfo = (infoString: string): RedisInfo => {
  const info: RedisInfo = {};

  const lines = infoString.split('\r\n');
  for (let i = 0; i < lines.length; ++i) {
    const parts = lines[i].split(':');
    if (parts[1]) {
      info[parts[0]] = parts[1];
    }
  }
  return info;
};

Source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.