0

I want to know some process information ran on the VPS with PM2. But, with the JSON string return by PM2, we can't run JSON.parse() because the JSON is broken.

An example of what PM2 returns:

'{data: 0, informations: "hello", name: "test"}'

But, if you know how the JSON.parse work, you know the problem. We can't parse this string.

Do you have idea to resolve this problem with another function ?

Thanks in advance.

Inigo
  • 12,186
  • 5
  • 41
  • 70
Pioupia
  • 339
  • 2
  • 15
  • Write your own parser or fix whatever generates that not-JSON string. – Pointy Jun 12 '21 at 21:10
  • Does this answer your question? [Javascript Regular expression to wrap unquoted JSON Values (NOT keys) with double quotes](https://stackoverflow.com/questions/45099150/javascript-regular-expression-to-wrap-unquoted-json-values-not-keys-with-doubl) – Kinglish Jun 12 '21 at 21:16

3 Answers3

4

It seems like your string is encoded in regular JavaScript or JSON5. JSON5 is a library which allows JSON to be defined in a similar way you would use it in regular JavaScript (without unnecessary ", etc.). Install the library and parse the string with JSON5.parse(). Reference: https://json5.org/

ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
1

Here is a solution for your specific non-json format. The trick is to make sure to have double quotes around keys...

function customParse(string){
  return JSON.parse(string
                    .replace(/\s|"/g, '')        // Removes all spaces and double quotes
                    .replace(/(\w+)/g, '"$1"') // Adds double quotes everywhere
                    .replace(/"(\d+)"/g, '$1')   // Removes double quotes around integers
                   )
}

// Testing the function
let PM2_string = '{data: 0, informations: "hello", name: "test"}'
let PM2_obj = customParse(PM2_string)

// Result
console.log(PM2_obj)
console.log(PM2_obj.data)
console.log(PM2_obj.informations)
console.log(PM2_obj.name)
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
  • This would probably turn true|false booleans into strings too, right? – Andreas Sep 24 '21 at 12:05
  • Sure @Andreas... This is a specific solution for a specific problem. In other words: a patch. Booleans, floating numbers, escaped double quotes in the original strings will still be problems (that was not requested here). – Louys Patrice Bessette Oct 03 '21 at 14:41
0

the string is in JSON5 format and not regular JSON format. To parse a JSON5 string in JavaScript, you will need to use a library that supports the JSON5 format, like json5.

install:

npm install json5
import json5 from 'json5';
const str = "{data: 0, informations: 'hello', name: 'test'}";
const obj = json5.parse(str);

By the way, you could also use Hjson to parse that string. JSON5 is a more widely adopted format than Hjson, and has been around for longer. Hjson, while still relatively popular, can be a good choice for configuration files where human readability and ease of use are a priority.

cwtuan
  • 1,718
  • 1
  • 18
  • 20