I need to add some text on the code through Javascript

Hello,

I have a json file which look like this:

[
  {
    "long": 20.744430042780497,
    "lat": 42.21149312068367,
    "lartesia": null,
    "lloji": null,
    "specia": null,
    "foto": null,
    "Tjeter": null
  },

but to show those informations in a library that i am using they need to look like this:

var pointJson = 
{
    "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "properties": {
            "name": "blla blla blla ",
            "lartesia": "10metra"
        },
        "geometry": {
          "coordinates": [
            20.744430042780497,
            42.21149312068367
          ],
          "type": "Point"
        }
      }},

So basically i need to add some properties and some coordinates to that, i know that u may say do it manually yeah u are kinda right, but i have 7k lines code in this json file with coordinates so i know a little bit that i need to use array map, but can somebody help me? or atleast any advice to it ?

Hi @camperextraordinaire,

I have json file with 7k line code which looks like this one:

[
  {
    "long": 20.744430042780497,
    "lat": 42.21149312068367,
    "lartesia": null,
    "lloji": null,
    "specia": null,
    "foto": null,
    "Tjeter": null
  },

So it means i have 7k line code with those types of information but in order to work for me in this framework that i am using i need to take those “Latersia”, “Lloji” , “Specia” etc… and to put in the object who look like this

"properties": {
            "name": "blla blla blla ",
             "lloji" : "blla blla blla",
             "lartesia": "10metra"
        },

and to add one more object for those coordinates with long and lattitude who looks like this :

"geometry": {
          "coordinates": [
            20.744430042780497,
            42.21149312068367
          ],
          "type": "Point"
        }

and not to look like this:

    "long": 20.744430042780497,
    "lat": 42.21149312068367,

So i just want to know if is there any solution to this, can i do this somehow with a javascript maybe with map array or something , because to do it manually it will take soo much time because i have 7k lines of code there one by one it will take too much time

The final object would look like this:

but for now i have them only like this :

Yeah u are right i just put that “name” just for example

This is the final object.

It appears all you really want to do is create a new array for the features property, so I would approach it this way:

const features = arr.map(obj => {
  const { long, lat, ...properties } = obj;
  return {
    type: "Feature",
    properties,
    geometry: {
      coordinates: [long, lat],
      type: "Point"
    }
  };
});

You can then put the features array as a property into any final object you want. You can then turn the object into JSON using JSON.stringify(obj) or if you prefer to have it formatted to make it, then use JSON.stringifiy(obj, null, 2).

Thank youu sooo muchhh :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.