RegEx to find json values without quotes


RegEx to find json values without quotes
I'm having an issue with a parser (in this case PowerBI) not taking values that are not quoted.
The example JSON file which comes from an API is validating ok, but PowerBI is not having any of it:
'requester_id': 361114274894, 'submitter_id': 361114274894, 'assignee_id': 361282665913, 'organization_id': 360009532534, 'group_id': 360000498954, 'name':'John Doe'
I'd like to find all values without quotes so I can replace them.
Please help.
JSON.stringify
It's the output I got from an API. It's not the full output of course, it's a subset, but the unquoted values are giving me an issue.
– R0b0tn1k
48 mins ago
Its just digits, match digits and replace them surrounded by quotes?
– UnbearableLightness
44 mins ago
If the api is outputting single quoted property keys that is invalid JSON. If the property keys are double quoted and numbers are not quoted that is perfectly valid. Beyond that you haven't provided a proper problem description and this sounds like an XY problem
– charlietfl
43 mins ago
Why would Power BI accept it? Again, it's not JSON. The problem is the API is not returning valid JSON, it's just returning JavaScript. String mangling is not a good fix, either fix the upstream API or explicitly stringify it to valid JSON in your layer. Use the built-in functionality, which will handle all of the edge cases for you.
– jonrsharpe
20 mins ago
4 Answers
4
You can use the for in loop. Assuming all non string items are numbers the following will work.
const obj =
a: 123,
b: "234234",
c: 09,
;
console.log(obj);
for (key in obj)
obj[key] = obj[key] + "";
console.log(obj);
The issue is with the use of single quotes instead of double quotes around the property names.
let correctedInput = input.replace(/'/g, '"');
let parsedInput = JSON.parse(correctedInput);
parsedInput
will now be your javascript object.
parsedInput
input.replace(/'/g, '"')
will find and replace all '
characters, and replace them with "
characters. This should work fine unless any of your property values have single quotes in them - For example, the name O'malley
will be parsed incorrectly.
input.replace(/'/g, '"')
'
"
O'malley
It seems like you'd like to convert your int values to string.
var json = 'requester_id': 361114274894, 'submitter_id': 361114274894, 'assignee_id': 361282665913, 'organization_id': 360009532534, 'group_id': 360000498954, 'name':'John Doe'
Object.keys(json).forEach(i => json[i]=json[i].toString());
// output
json
assignee_id:"361282665913"
group_id:"360000498954"
name:"John Doe"
organization_id:"360009532534"
requester_id:"361114274894"
submitter_id:"361114274894"
You can perform the regex pattern matching as below in Java
String rtype="^"|"$";
String value = ((JSONObject) nameOfObject).get("key").toString().matches(rtype);
or
((JSONObject) nameOfObject).get("key").toString().replaceAll(rtype);
he is not talking about Java but JavaScript. They are two complete different things.
– quirimmo
22 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
That's not JSON, it's JavaScript. You need to
JSON.stringify
it before writing the file.– jonrsharpe
49 mins ago