-1

I am trying to convert the below string value to JSON:

var yourmsg =  '{"yourid":{"latlng":[123,456],"data":{"id":2345," name ":" basanta ","status":"Available"}}}';

Please help me out.

  • can you suggest me the code – basanta Chapagai Jul 17 '15 at 23:30
  • var yourmsg = '{"yourid":{"latlng":[123,456],"data":{"id":2345," name ":" basanta ","status":"Available"}}}'; please convert this string into JSON – basanta Chapagai Jul 17 '15 at 23:58
  • It's unclear to me what you are trying to achieve. The value of the string already is JSON. – Felix Kling Jul 18 '15 at 00:03
  • Yes it is in JSON but as a string.... Can we change the string into JSON object – basanta Chapagai Jul 18 '15 at 00:06
  • thankz it worked with JSON.parse() – basanta Chapagai Jul 18 '15 at 00:19
  • JSON is a format for representing JavaScript objects, often as a string. Your `yourmsg` is **already** JSON. What I suspect you want to do is to convert it into a JavaScript object. As other comments and answers have already pointed out, that's exactly what `JSON.parse` does. That would come up on the first page, or more likely the first paragraph, of any document/tutorial on JSON. –  Jul 18 '15 at 03:48
  • @basantaChapagai: [There is no such thing as a JSON object](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). – Felix Kling Jul 18 '15 at 14:55

3 Answers3

0

All object keys need to be strings, you can't use yourid as key without qoutation marks:

var yourmsg =  '{"'+yourid + '":{"latlng")+:[' + yourlat + ','+ yourlng + '],"data":{"id":' + yourid +'," name ":" basanta ","status":"Available"}}}';
Félix
  • 1,685
  • 2
  • 11
  • 6
  • can you correct this code for me ??? – basanta Chapagai Jul 17 '15 at 23:41
  • var yourmsg = '{"yourid":{"latlng":[123,456],"data":{"id":2345," name ":" basanta ","status":"Available"}}}'; please convert this string into JSON – basanta Chapagai Jul 17 '15 at 23:58
  • This is not a "do it for me" forum. Just ask a question and I will try to help you. – Félix Jul 18 '15 at 00:21
  • And this would be different from the original `yourmsg` in the OP's question how? Creating a JSON string like this is an huge anti-pattern. It the problem is how to **create** a JSON string containing variables, then simply create the JavaScript object in the normal way and `JSON.stringify` it. However, that does not seem to be what the OP is trying to do. He **already has** a JSON string, and seems to want to turn it into a JS object. –  Jul 18 '15 at 03:50
0

This is how you convert your string to a JSON object and write it to a file:

var yourmsg = '{"yourid":{"latlng":[123,456],"data":{"id":2345," name ":" basanta ","status":"Available"}}}';

yourmsg = JSON.parse(yourmsg); // converts the string to JSON object.

fs.writeFile('file.json', JSON.stringify(yourmsg, null, '\t')); // write to file with tabbed formatting.
Ivan
  • 174
  • 5
0

Based on the original version of the question, I think what the OP really wanted was a way to create JavaScript objects based on three parameters (yourid, yourlat, yourlng). The OP's attempt was to first create a parameterized string and then ask how to get an object out of it.

If so, here is a more elegant approach to skip straight to the object creation and avoid needing to create a string and the use JSON.parse():

http://jsbin.com/qebiti/edit?js,console

var msgFactory = (function () {
    return function (id, lat, lng) {
        //id, lat, lng must be numbers because strings need to be quoted
        var msgObject = {};
        msgObject[id] = {
            latlng: [lat, lng],
            data: {
              id: id,
              name: " basanta ",
              status: "Available"
            }
        };
        return msgObject;
      };      
    }()),
    yourid = 2345,
    yourlat = 123,
    yourlng = 456,
    yourmsgObj = msgFactory(yourid, yourlat, yourlng);
console.log(JSON.stringify(yourmsgObj, null, 4));

The factory is based on the assumption (from the sample data in comments) that the parameters are all numbers. If strings are desired, then the factory should quote the strings.

The only tricky part was that yourid was being used both as a name and a value in the resultant object. This seems to have been lost in the question edits.

Jeremy Larter
  • 548
  • 2
  • 10
  • 21