-2

I am facing a problem. I have this json log

{
  "log": "Log Info     : { \"datetime\" : \"datetime\", \"field1\" : \"value1\", \"field2\" : \"value2\", \"field3\" : \"value3\", \"field4\" : \"value4\", \"field5\" : \"value5\", \"field6\" : \"value6\", \"field7\" : \"value7\", \"field8\" : \"value8\", \"field9\" : \"value9\", \"field10\" : \"value10\", \"field11\" : \"value11\"}\n",
  "stream": "stdout",
  "kubernetes": {
    "pod_name": "pod_name",
    "namespace_name": "namespace_name",
    "pod_id": "pod_id",
    "host": "host",
    "container_name": "container_name",
    "docker_id": "docker_id",
    "container_hash": "container_hash",
    "container_image": "container_image"
  }
}

I need to get all field inside "log" key. These fields will be increased, so I need to get all fields inside log dynamically. I am using this code to parse json, but the output is this. Maybe someone can help me? Thanks.

const readFile = require("fs").readFile;

readFile("log.json", (err, data) => {
  if (err) throw err;
  const log = JSON.parse(data);
  console.log(log);
});

Output:

{
  log: 'Log Info     : { "datetime" : "datetime", "field1" : "value1", "
field2" : "value2", "field3" : "value3", "field4" : "value4", "field5" :
 "value5", "field6" : "value6", "field7" : "value7", "field8" : "value8"
, "field9" : "value9", "field10" : "value10", "field11" : "value11"}\n',
  stream: 'stdout',
  kubernetes: {
    pod_name: 'pod_name',
    namespace_name: 'namespace_name',
    pod_id: 'pod_id',
    host: 'host',
    container_name: 'container_name',
    docker_id: 'docker_id',
    container_hash: 'container_hash',
    container_image: 'container_image'
  }
}
Antonio Costa
  • 101
  • 1
  • 6
  • Why is the value of `log` this strange "thing" that should be just another object and instead is JSON with some prefix which makes it invalid JSON? – Andreas Oct 17 '22 at 15:23
  • 2
    That's an unfortunate format, since the value of `log` is not valid JSON, given the `'Log Info : ` text. You'd have to use substring manipulation to extricate the string between `{` and `}` and parse that as JSON. – Heretic Monkey Oct 17 '22 at 15:25
  • 1
    What have you tried so far to solve this on your own? -> [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Andreas Oct 17 '22 at 15:25
  • I have tried using regex as well. – Antonio Costa Oct 17 '22 at 15:27
  • Does this answer your question? [Parsing JSON from string containing JSON object](https://stackoverflow.com/questions/39480284/parsing-json-from-string-containing-json-object) – Heretic Monkey Oct 17 '22 at 15:31
  • Yes, it answers my question I am having another problem with the original json, with the original fields, but for now, this solves my problem. Thank you for the help. – Antonio Costa Oct 17 '22 at 15:39

1 Answers1

1

You need to JSON.parse also the "inner" json (after you clean it from the prefix)

var obj = {
  "log": "Log Info     : { \"datetime\" : \"datetime\", \"field1\" : \"value1\", \"field2\" : \"value2\", \"field3\" : \"value3\", \"field4\" : \"value4\", \"field5\" : \"value5\", \"field6\" : \"value6\", \"field7\" : \"value7\", \"field8\" : \"value8\", \"field9\" : \"value9\", \"field10\" : \"value10\", \"field11\" : \"value11\"}\n",
  "stream": "stdout",
  "kubernetes": {
    "pod_name": "pod_name",
    "namespace_name": "namespace_name",
    "pod_id": "pod_id",
    "host": "host",
    "container_name": "container_name",
    "docker_id": "docker_id",
    "container_hash": "container_hash",
    "container_image": "container_image"
  }
}

var str = (obj.log).substring(obj.log.indexOf(':') + 1);
//console.log(str)
var result = JSON.parse(str);
console.log(result)
IT goldman
  • 14,885
  • 2
  • 14
  • 28