I'm trying to scrape a .JSON file however the returned file returns multiple JSON objects in 1 .json file, resulting in a invalid json file. I'm trying to solve this by adding a [ before the JSON file and a ] after the json file. Then using a regex to add comma at the right places.
This is the file before the regex
[{"status":"success"}
{"values":{"cpu":26.5152886753948387,"ram":0.8452846061135513},"origin":"core","type":-1,"uuid":"0000-e8-de-27-176d10"}
{"values":{"cpu":25.5839236550189568,"ram":0.8452846061135513},"origin":"core","type":-1,"uuid":"0000-e8-de-27-176d10"}
]
I created the following regex: }(?!\s,|\s])
. The problem I have is that it is still adding a , after the last } even though it's followed by a ].
What I'm getting:
[{"status":"success"},
{"values":{"cpu":26.5152886753948387,"ram":0.8452846061135513},"origin":"core","type":-1,"uuid":"0000-e8-de-27-176d10"},
{"values":{"cpu":25.5839236550189568,"ram":0.8452846061135513},"origin":"core","type":-1,"uuid":"0000-e8-de-27-176d10"},
]
Expected result:
[{"status":"success"},
{"values":{"cpu":26.5152886753948387,"ram":0.8452846061135513},"origin":"core","type":-1,"uuid":"0000-e8-de-27-176d10"},
{"values":{"cpu":25.5839236550189568,"ram":0.8452846061135513},"origin":"core","type":-1,"uuid":"0000-e8-de-27-176d10"}
]
Replace \s
in your regex with \s*
since \s
will match a single space character where \s*
would match zero or more space characters.
}(?!\s*[,\]])
$re = "/}(?!\\s*[,\\]])/m";
$str = "[{\"status\":\"success\"}
{\"values\":{\"cpu\":26.5152886753948387,\"ram\":0.8452846061135513},\"origin\":\"core\",\"type\":-1,\"uuid\":\"0000-e8-de-27-176d10\"}
{\"values\":{\"cpu\":25.5839236550189568,\"ram\":0.8452846061135513},\"origin\":\"core\",\"type\":-1,\"uuid\":\"0000-e8-de-27-176d10\"}
]";
$subst = "},";
$result = preg_replace($re, $subst, $str);