1

I have an array of objects as follows:

[
  {
    "type": "Feature",
    "geometry": {
      "type": "Point",
      "coordinates": [
        137.89094924926758,
        36.93143814715343
      ]
    },
    "properties": {
      "@geometry": "center",
      "@id": "way/323049815",
      "id": "way/323049815",
      "landuse": "winter_sports",
      "name": "糸魚川シーサイドバレースキー場",
      "name:en": "Itoigawa Seaside Valley Ski Resort",
      "name:ja": "糸魚川シーサイドバレースキー場",
      "source": "Bing",
      "sport": "skiing",
      "website": "https://www.seasidevalley.com/",
      "wikidata": "Q11604871",
      "wikipedia": "ja:糸魚川シーサイドバレースキー場"
    },
    
    [snip]

I want to add the above array into a data object in javascript as follows.

{
    "data": [
        //my data here.
    ]
}

I have tried this;

          let mydata = {
            "data": skidata
          }

but is places back slashed a lot like this snippet;

{
    "data": "[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\"...

How do I remove the back slashes in javascript please?

This is the specific code;

            let skidata = JSON.stringify(uniqueFeatures);
    
            let mydata = {
                "data": skidata
            }
            console.log((mydata));

When I console.log skidata, there are no backslashes. When I console.log mydata, back slashes are added, I think. This is a pict of console.log(mydata)

screen shot of console for my data

3
  • That's not what happens for the code you posted. The problem is somewhere in the code you did not include. Please be thorough, or you won't get the help you're looking for. Commented Dec 3, 2022 at 3:57
  • @MichaelG thanks for your review. I have pasted more of the code and a screenshot. Possible to please take a look? Commented Dec 3, 2022 at 5:31
  • 1
    Is there a reason you wanted to use JSON.stringify()? Are you trying to POST this to a server or something similar? Commented Dec 3, 2022 at 14:14

1 Answer 1

1

Don't use JSON.stringify(uniqueFeatures).

That turns your object into a string.

Instead of doing that, just use let mydata1 = { "data": uniqueFeatures };.

Demo:

let uniqueFeatures = [{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [
      137.89094924926758,
      36.93143814715343
    ]
  },
  "properties": {
    "@geometry": "center",
    "@id": "way/323049815",
    "id": "way/323049815",
    "landuse": "winter_sports",
    "name": "糸魚川シーサイドバレースキー場",
    "name:en": "Itoigawa Seaside Valley Ski Resort",
    "name:ja": "糸魚川シーサイドバレースキー場",
    "source": "Bing",
    "sport": "skiing",
    "website": "https://www.seasidevalley.com/",
    "wikidata": "Q11604871",
    "wikipedia": "ja:糸魚川シーサイドバレースキー場"
  }
}];

let skidata = JSON.stringify(uniqueFeatures);

let mydata1 = {
  "data": uniqueFeatures
};

let mydata2 = {
  "data": skidata
};

console.log("What you get if you stringify your object:");
console.log(mydata2);

console.log("What you want, instead:");
console.log(mydata1);
<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Demo</title>
</head>

<body>

</body>

</html>

Reference: JSON.stringify()

Sign up to request clarification or add additional context in comments.

3 Comments

JS does not add backslashes to escape double quotes. The console does when printing strings. That does not affect his code. (E.g. the length of ('Str"i"ng').length === 7. If JS added backslashes, it would be length 9.)
@MichaelG - Thank you for the correction. What I wrote was wrong. (Your string length is 8 not 7, right?)
LOL yep. Apparently can't count. :-)

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.