0

Background

I have a string containing a javascript object. This string is not JSON stringified ( only a portion of it is ).

I need to convert this string into an object so than I can use it.

Here is an example of such string:

`{method: 'POST', url: '/iot/pipe/', query: {}, body: { d: '{"n": "861359031087669", "b": 100, "v": "02.37", "t": 1, "d":[[1515070895,413973366,21717600,110,1,0],[1515070897,413975033,21719083,102,1,0]]}' }, headers: { host: 'node_session_iot', connection: 'close', 'content-length': '1219', accept: '*/*', 'user-agent': 'QUECTEL_MODULE', 'content-type': 'application/x-www-form-urlencoded' } }`

JSON.parse

The string is not json stringified, so parse will fail.

eval

eval is evil. Never use it.

Solutions?

I find it incredibly frustrating. I have the object right in front of me and I can't do a single thing with it. What other options do I have to convert this string into an object?

9
  • 1
    How can this be a String ? You have some double quotes in there that are breaking the whole String. Commented Mar 16, 2018 at 9:53
  • Maybe you can use regex expression to extract the info you need, then use the object constructor if available to get the object. Or remove the useless parts using regex as well then parse it? Commented Mar 16, 2018 at 9:54
  • 1
    What you have is a Javascript literal. You're not supposed to have those as a string; that's specifically what JSON is for, which is a safe subset of Javascript. You either need a Javascript parser, or you take the plunge and evil it. Preferably you could go back and not produce such strings in the first place. Commented Mar 16, 2018 at 9:55
  • @Zenoo Copy paste issue. The characters at begin and end should be ``` instead of ". Fixing. Commented Mar 16, 2018 at 9:56
  • @nespapu I spend the last hour of my life writting a parser only yo find out it produces ugly and unstable code. The problem with parsers is that any changes to the format of the string will completely obliterate the code. Commented Mar 16, 2018 at 9:58

2 Answers 2

3

Well I wont say it is a perfect solution and it is very example specific but idea is to convert string to step by step to JSON string

Hope it works

//take in quotation
y = x.replace(/(\w+)(\s*:)+/g,"\"\$1\"$2");
//convert single quotation into "
y = y.replace(/\'/g,"\"" );
// remove " from object literals
y = y.replace(/\"\s*{/g,"{" );
y = y.replace(/}\"\s*/g,"}" );
yOjb = JSON.parse(y);
Sign up to request clarification or add additional context in comments.

1 Comment

Naive, but it works ! Amazing out of the box thinking !
0

Your request body is in correct JSON format. So take request body from the given string by using regular expression or string functions and use JSON.Parse method to get the object.

In this case your request body is

{"n": "861359031087669", "b": 100, "v": "02.37", "t": 1, "d":[[1515070895,413973366,21717600,110,1,0],[1515070897,413975033,21719083,102,1,0]]}

Please see the below snapshot for the JSON.Parse function. enter image description here

2 Comments

Yes, but if I do it that way, I literally loose access to all the other information....
If you can provide a regex to pick just the body of the expression, I will accept your answer.

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.