0

I am working with a JSON array and trying to do a simple parse and cannot get the following line of code to work:

      document.getElementById('MapId').value = obj[0].properties.OBJECTID;

This does not populate the input box on the form.

Should I be using stringify instead?

Here's my document below

    <!DOCTYPE html>
     <html>
     <body>

        <h2>Create Object from JSON String</h2>

        <p id="demo"></p>
        <form>
            <h1>
                Map Title
            </h1>
            Id:<br>
            <input type="text" name="MapId" id="MapId">
        </form>

        <script>

            var obj = JSON.parse('[
      {
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": [
                            [
                                [
                                    -80.38001898676157,
                                    43.41005948475032
                                ],
                                [
                                    -80.38005586713552,
                                    43.41005583130982
                                ],
                                [
                                    -80.38005754351616,
                                    43.410064843129305
                                ],
                                [
                                    -80.3800206631422,
                                    43.41006849656927
                                ],
                                [
                                    -80.38001898676157,
                                    43.41005948475032
                                ]
                            ]
                        ]
                    },
                    "type": "Feature",
                    "properties": {
                        "DIRECTION": "0",
                        "OBJECTID": 1309,
                        "RANGE": "000",
                        "SECTION_": "15",
                        "SHAPE_AREA": 3.030074,
                        "SHAPE_LENG": 8.040001
                    },
                    "id": 97,
                    "layer": {
                        "id": "cemeterypl3-6k66v9",
                        "type": "fill",
                        "source": "composite",
                        "source-layer": "CemeteryPL3-6k66v9",
                        "layout": {
                            "visibility": "visible"
                        },
                        "paint": {
                            "fill-opacity": 0.5,
                            "fill-color": "hsl(107, 50%, 27%)"
                        }
                    }
                }
    ]');
            document.getElementById('MapId').value = obj[0].properties.OBJECTID;
        </script>

    </body>
    </html>

tenter image description here

3 Answers 3

2

You would have been getting this error in your browser's console

Uncaught SyntaxError: Invalid or unexpected token

You are specifying the multi-line string (template literals) wrongly in your code, you need to specify the String using **** instead of'`.

JSON.parse(` ... ` )

instead of

JSON.parse(' ... ');

Check here how to create multi-line string using '

Demo

<!DOCTYPE html>
<html>

<body>

  <h2>Create Object from JSON String</h2>

  <p id="demo"></p>
  <form>
    <h1>
      Map Title
    </h1>
    Id:<br>
    <input type="text" name="MapId" id="MapId">
  </form>

  <script>
    var obj = JSON.parse(`[
      {
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": [
                            [
                                [
                                    -80.38001898676157,
                                    43.41005948475032
                                ],
                                [
                                    -80.38005586713552,
                                    43.41005583130982
                                ],
                                [
                                    -80.38005754351616,
                                    43.410064843129305
                                ],
                                [
                                    -80.3800206631422,
                                    43.41006849656927
                                ],
                                [
                                    -80.38001898676157,
                                    43.41005948475032
                                ]
                            ]
                        ]
                    },
                    "type": "Feature",
                    "properties": {
                        "DIRECTION": "0",
                        "OBJECTID": 1309,
                        "RANGE": "000",
                        "SECTION_": "15",
                        "SHAPE_AREA": 3.030074,
                        "SHAPE_LENG": 8.040001
                    },
                    "id": 97,
                    "layer": {
                        "id": "cemeterypl3-6k66v9",
                        "type": "fill",
                        "source": "composite",
                        "source-layer": "CemeteryPL3-6k66v9",
                        "layout": {
                            "visibility": "visible"
                        },
                        "paint": {
                            "fill-opacity": 0.5,
                            "fill-color": "hsl(107, 50%, 27%)"
                        }
                    }
                }
    ]`);
    document.getElementById('MapId').value = obj[0].properties.OBJECTID;
  </script>

</body>

</html>

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

4 Comments

@Kaddath You can create a multi-line string using ' as well.
Isn't multiline string only supported in ES6? Does it work in current browsers?
@ArthurCantarela Browser support is mentioned in the link I have shared. It definitely works in current browsers. You can try the demo I have shared.
@Kaddath I agree that even though it is achievable with ', but ' is not meant for multi-line.
2

That happening because the Json parse function only accepts a string, and when you set a new line in your string Js parse that line as a new line of code, causing the error.

var obj = JSON.parse('[{"geometry": {"type": "Polygon","coordinates": [[[-80.38001898676157,43.41005948475032], [-80.38005586713552,43.41005583130982],[-80.38005754351616,43.410064843129305],[-80.3800206631422,43.41006849656927],[-80.38001898676157,43.41005948475032]]]},"type": "Feature","properties": {"DIRECTION": "0","OBJECTID": 1309,"RANGE": "000","SECTION_": "15","SHAPE_AREA": 3.030074,"SHAPE_LENG": 8.040001},"id": 97,"layer": {"id": "cemeterypl3-6k66v9", "type":"fill","source": "composite","source-layer": "CemeteryPL3-6k66v9","layout": {"visibility": "visible"},"paint": {"fill-opacity": 0.5,"fill-color": "hsl(107, 50%, 27%)"}}}]');
document.getElementById('MapId').value = obj[0].properties.OBJECTID;
        <h2>Create Object from JSON String</h2>

        <p id="demo"></p>
        <form>
            <h1>
                Map Title
            </h1>
            Id:<br>
            <input type="text" name="MapId" id="MapId">
        </form>

    
 

Comments

0

No need to parse to json if you use an array with objects. You can remove the JSON.parse

    var obj = [{"geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                -80.38001898676157,
                                43.41005948475032
                            ],
                            [
                                -80.38005586713552,
                                43.41005583130982
                            ],
                            [
                                -80.38005754351616,
                                43.410064843129305
                            ],
                            [
                                -80.3800206631422,
                                43.41006849656927
                            ],
                            [
                                -80.38001898676157,
                                43.41005948475032
                            ]
                        ]
                    ]
                },
                "type": "Feature",
                "properties": {
                    "DIRECTION": "0",
                    "OBJECTID": 1309,
                    "RANGE": "000",
                    "SECTION_": "15",
                    "SHAPE_AREA": 3.030074,
                    "SHAPE_LENG": 8.040001
                },
                "id": 97,
                "layer": {
                    "id": "cemeterypl3-6k66v9",
                    "type": "fill",
                    "source": "composite",
                    "source-layer": "CemeteryPL3-6k66v9",
                    "layout": {
                        "visibility": "visible"
                    },
                    "paint": {
                        "fill-opacity": 0.5,
                        "fill-color": "hsl(107, 50%, 27%)"
                    }
                }
            }
];

1 Comment

You are right Kaddath, i did change the first sentence.

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.