Announcement

Collapse
No announcement yet.

Api call, Ergebnis umbauen und in json Datei speichern

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Api call, Ergebnis umbauen und in json Datei speichern

    Hallo zusammen, bin hier irwie am verzweifeln, vorweg ich bin noch nicht so lange dabei mich mit js auseinander zu setzen.

    Ich frage eine Api ab und möchte dies in einer json Datei speichern. klappt auch soweit ganz gut, doch ist das was gespeichert wird zu viel für mich, also nicht relevantes Zeug.

    (Das ganze ist in einem nodejs script)

    API call:
    Code:
      function getPriceList() {
        request(priceUrl, (error, response, body) => {
            if (error || response.statusCode !== 200) return console.log(`Error: ${error} - Status Code: ${response.statusCode}`);
            //console.log("DEBUG: " + body);
            fs.writeFileSync("prices.json", JSON.stringify(JSON.parse(body), null, 2));
    
        });
    }
    getPriceList();
    Snippet (Original ca.300000 Zeilen) aus der erstellten "prices.json" Datei:
    Code:
      {
      "status": "success",
      "prices": [
        {
          "app_id": "730",
          "context_id": "2",
          "market_hash_name": "Blueberries Buckshot | NSWC SEAL",
          "price": "1.71",
          "pricing_mode": "market",
          "skewness": "-0.49",
          "created_at": 1631410301,
          "icon_url": null,
          "name_color": null,
          "quality_color": null,
          "rarity_color": null,
          "instant_sale_price": null
        },
        {
          "app_id": "730",
          "context_id": "2",
          "market_hash_name": "The Doctor Romanov | Sabre",
          "price": "3.06",
          "pricing_mode": "market",
          "skewness": "-0.05",
          "created_at": 1631419491,
          "icon_url": null,
          "name_color": null,
          "quality_color": null,
          "rarity_color": null,
          "instant_sale_price": "1.22"
        },
        {
          "app_id": "730",
          "context_id": "2",
          "market_hash_name": "Two Times McCoy | TACP Cavalry",
          "price": "1.23",
          "pricing_mode": "market",
          "skewness": "-0.11",
          "created_at": 1631407484,
          "icon_url": null,
          "name_color": null,
          "quality_color": null,
          "rarity_color": null,
          "instant_sale_price": null
        }
      ]
    }
    Meine Frage nun, ist es irgendiwe möglich, das nur noch dies:

    Code:
    {
      "Blueberries Buckshot | NSWC SEAL": 1.71,
      "The Doctor Romanov | Sabre": 3.06,
      "Two Times McCoy | TACP Cavalry": 1.23
    }
    in der "prices.json" gespeichert wird?

    Also quasi alle

    "market_hash_name": price

    in diesem Format.

    mfG

    André
    Zuletzt editiert von CellSplitter; 16.09.2021, 02:22.

  • #2
    https://www.w3schools.com/js/js_json_parse.asp
    In eine Liste von Objekten wandeln, diese durchlaufen und nur die gewünschten Elemente speichern
    Christian

    Comment


    • #3
      Vielen Dank für die Antwort, ich bin damit auch schon weiter gekommen, aber noch nicht ganz am Ziel.

      Nehmen wir mal an wir hätten die Konstante:
      Code:
      const body = {
        "status": "success",
        "prices": [
          {
            "app_id": "730",
            "context_id": "2",
            "market_hash_name": "Blueberries Buckshot | NSWC SEAL",
            "price": "1.71",
            "pricing_mode": "market",
            "skewness": "-0.49",
            "created_at": 1631410301,
            "icon_url": null,
            "name_color": null,
            "quality_color": null,
            "rarity_color": null,
            "instant_sale_price": null
          }
        ]
      };
      Ist wohl nicht ganz sauber geschrieben so, aber ich komme an mein Ziel:
      Code:
      const test = JSON.stringify(body.prices);
      //console.log(test);
      const replace1 = test.replace("[", "");
      //console.log(replace1);
      const replace2 = replace1.replace("]", "");
      //console.log(replace2);
      const text = replace2;
      //console.log(text);
      const obj = JSON.parse(text);
      console.log('{"' + obj.market_hash_name + '": ' + obj.price + '}');
      Dort würde jetzt genau das bei raus kommen wie ich is möchte
      Code:
      {
      "Blueberries Buckshot | NSWC SEAL": 1.71
      }
      Doch sieht die Konstannte so aus, das der market_hash_name und price öfter drin vor kommt, so wie es eigentlich ist:
      Code:
      const body = {
        "status": "success",
        "prices": [
          {
            "app_id": "730",
            "context_id": "2",
            "market_hash_name": "Blueberries Buckshot | NSWC SEAL",
            "price": "1.71",
            "pricing_mode": "market",
            "skewness": "-0.49",
            "created_at": 1631410301,
            "icon_url": null,
            "name_color": null,
            "quality_color": null,
            "rarity_color": null,
            "instant_sale_price": null
          },
          {
            "app_id": "730",
            "context_id": "2",
            "market_hash_name": "The Doctor Romanov | Sabre",
            "price": "3.06",
            "pricing_mode": "market",
            "skewness": "-0.05",
            "created_at": 1631419491,
            "icon_url": null,
            "name_color": null,
            "quality_color": null,
            "rarity_color": null,
            "instant_sale_price": "1.22"
          },
          {
            "app_id": "730",
            "context_id": "2",
            "market_hash_name": "Two Times McCoy | TACP Cavalry",
            "price": "1.23",
            "pricing_mode": "market",
            "skewness": "-0.11",
            "created_at": 1631407484,
            "icon_url": null,
            "name_color": null,
            "quality_color": null,
            "rarity_color": null,
            "instant_sale_price": null
          }
        ]
      };
      klappt es leider nicht mehr und es kommt nur NULL bei raus.

      Wie schaffe ich es das er alle auflistet wie hier?
      Code:
      {
        "Blueberries Buckshot | NSWC SEAL": 1.71,
        "The Doctor Romanov | Sabre": 3.06,
        "Two Times McCoy | TACP Cavalry": 1.23
      }
      wenn das noch irgendwie klappen würde bin ich glücklich

      Comment


      • #4
        Die Daten sind nicht mehrfach drin, vielmehr hast du mehrfache prices Objekte.
        Jedes für sich hat einmal market_hash_name und price.

        Die Theorie wäre - ich habe das nicht ausprobiert - das JSON.parse(text); nicht 1 Objekt erzeugt, wenn es auf die mehrfachen prices Objekte losgelassen wird, sondern ein Array von Objekten.
        Also
        objList = JSON.parse(text);
        zeile=objList.prices[0].market_hash_name+"|"+objList.prices[0].price+",";
        Und das mit einer Schleife -> foreach

        Das ist hier doch genau beschrieben
        https://www.w3schools.com/js/js_json.asp

        let text = '{ "employees" : [' +
        '{ "firstName":"John" , "lastName":"Doe" },' +
        '{ "firstName":"Anna" , "lastName":"Smith" },' +
        '{ "firstName":"Peter" , "lastName":"Jones" } ]}';

        mehrfache Namen (Vor- und Nachname)
        Wandeln in Object
        const obj = JSON.parse(text);

        Zugriff über Index
        obj.employees[1].firstName + " " + obj.employees[1].lastName;
        Zuletzt editiert von Christian Marquardt; 16.09.2021, 14:39.
        Christian

        Comment

        Working...
        X