0

When I use JSON.parse(myJSONString) the result is not a valid JSON file. I'm in an nodejs environment. Any Ideas what I can fix in my code? I user XML2JS to converte an XML Api call to JS.

I have printed out the results and already dived deeper into JSON.parse but didn't find further information...

exports.getStations = functions.https.onCall(async (data) => {
    let stations

    await axios.get(encodeURI(`XMLAPi`))
        .then(async (response) => {
            parseString(response.data, (err, result) => {
                stations = JSON.stringify(result)
                console.log(stations) //First console.log
            }) 
        })
        .catch((error) => {
            console.log(error)
        })
        
    console.log(JSON.parse(stations)) //Second console.log
    return JSON.parse(stations)
})

Result for first console.log (valid JSON but String):

{"itdRequest":{"$":{"xsi:noNamespaceSchemaLocation":"itd.xsd","language":"DE","sessionID":"EfaOpenServiceT_2882840828","client":"axios/0.19.0","serverID":"EfaOpenServiceT_","clientIP":"127.0.0.1","version":"10.3.5.46","virtDir":"static02","xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance"},"itdStopFinderRequest":{"$":{"requestID":"1"},"itdOdv":{"$":{"type":"any","usage":"sf","anyObjFilter":"42"},"itdOdvPlace":{"$":{"state":"empty"},"odvPlaceElem":""},"itdOdvName":{"$":{"state":"list"},"itdMessage":{"$":{"type":"error","module":"BROKER","code":"-8011"}},"odvNameElem":[{"_":"Mettmann, Stadtwald S","$":{"value":"0:1","selected":"1","listIndex":"0","streetName":"","omc":"5158024","placeID":"5","x":"6987248.99348","y":"51250980.94198","mapName":"WGS84","id":"20019083","anyType":"stop","anyTypeSort":"2","nameKey":"","locality":"Mettmann","postCode":"","objectName":"Stadtwald S","buildingName":"","buildingNumber":"","matchQuality":"235","stateless":"20019083"}},{"_":"Mettmann, ME-Zentrum S","$":{"value":"1:2","selected":"0","listIndex":"1","streetName":"","omc":"5158024","placeID":"5","x":"6979002.45917","y":"51249372.83702","mapName":"WGS84","id":"20019020","anyType":"stop","anyTypeSort":"2","nameKey":"","locality":"Mettmann","postCode":"","objectName":"ME-Zentrum S","buildingName":"","buildingNumber":"","matchQuality":"234","stateless":"20019020"}},{"_":"Mettmann, Neanderthal S","$":{"value":"2:3","selected":"0","listIndex":"2","streetName":"","omc":"5158024","placeID":"5","x":"6953346.57466","y":"51227837.93128","mapName":"WGS84","id":"20019191","anyType":"stop","anyTypeSort":"2","nameKey":"","locality":"Mettmann","postCode":"","objectName":"Neanderthal S","buildingName":"","buildingNumber":"","matchQuality":"234","stateless":"20019191"}}],"odvNameInput":"mettmann"},"genAttrList":{"genAttrElem":[{"name":"anyObjFilter","value":"STOP"},{"name":"anyObjFilter","value":"ADDRESS"},{"name":"anyObjFilter","value":"POI"}]}},"itdDateTime":{"$":{"ttpFrom":"20190601","ttpTo":"20191231"},"itdDate":{"$":{"day":"10","month":"8","year":"2019","weekday":"7"}},"itdTime":{"$":{"hour":"16","minute":"4"}}}}}}

Result for second console.log (invalid JSON after parse):

Object {
  "data": Object {
    "itdRequest": Object {
      "$": Object {
        "client": "axios/0.19.0",
        "clientIP": "127.0.0.1",
        "language": "DE",
        "serverID": "EfaOpenServiceT_",
        "sessionID": "EfaOpenServiceT_2882840828",
        "version": "10.3.5.46",
        "virtDir": "static02",
        "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "xsi:noNamespaceSchemaLocation": "itd.xsd",
      },
      "itdStopFinderRequest": Object {
        "$": Object {
          "requestID": "1",
        },
        "itdDateTime": Object {
          "$": Object {
            "ttpFrom": "20190601",
            "ttpTo": "20191231",
          },
          "itdDate": Object {
            "$": Object {
              "day": "10",
              "month": "8",
              "weekday": "7",
              "year": "2019",
            },
          },
          "itdTime": Object {
            "$": Object {
              "hour": "16",
              "minute": "4",
            },
          },
        },
        "itdOdv": Object {
          "$": Object {
            "anyObjFilter": "42",
            "type": "any",
            "usage": "sf",
          },
          "genAttrList": Object {
            "genAttrElem": Array [
              Object {
                "name": "anyObjFilter",
                "value": "STOP",
              },
              Object {
                "name": "anyObjFilter",
                "value": "ADDRESS",
              },
              Object {
                "name": "anyObjFilter",
                "value": "POI",
              },
            ],
          },
          "itdOdvName": Object {
            "$": Object {
              "state": "list",
            },
            "itdMessage": Object {
              "$": Object {
                "code": "-8011",
                "module": "BROKER",
                "type": "error",
              },
            },
            "odvNameElem": Array [
              Object {
                "$": Object {
                  "anyType": "stop",
                  "anyTypeSort": "2",
                  "buildingName": "",
                  "buildingNumber": "",
                  "id": "20019083",
                  "listIndex": "0",
                  "locality": "Mettmann",
                  "mapName": "WGS84",
                  "matchQuality": "235",
                  "nameKey": "",
                  "objectName": "Stadtwald S",
                  "omc": "5158024",
                  "placeID": "5",
                  "postCode": "",
                  "selected": "1",
                  "stateless": "20019083",
                  "streetName": "",
                  "value": "0:1",
                  "x": "6987248.99348",
                  "y": "51250980.94198",
                },
                "_": "Mettmann, Stadtwald S",
              },
              Object {
                "$": Object {
                  "anyType": "stop",
                  "anyTypeSort": "2",
                  "buildingName": "",
                  "buildingNumber": "",
                  "id": "20019020",
                  "listIndex": "1",
                  "locality": "Mettmann",
                  "mapName": "WGS84",
                  "matchQuality": "234",
                  "nameKey": "",
                  "objectName": "ME-Zentrum S",
                  "omc": "5158024",
                  "placeID": "5",
                  "postCode": "",
                  "selected": "0",
                  "stateless": "20019020",
                  "streetName": "",
                  "value": "1:2",
                  "x": "6979002.45917",
                  "y": "51249372.83702",
                },
                "_": "Mettmann, ME-Zentrum S",
              },
              Object {
                "$": Object {
                  "anyType": "stop",
                  "anyTypeSort": "2",
                  "buildingName": "",
                  "buildingNumber": "",
                  "id": "20019191",
                  "listIndex": "2",
                  "locality": "Mettmann",
                  "mapName": "WGS84",
                  "matchQuality": "234",
                  "nameKey": "",
                  "objectName": "Neanderthal S",
                  "omc": "5158024",
                  "placeID": "5",
                  "postCode": "",
                  "selected": "0",
                  "stateless": "20019191",
                  "streetName": "",
                  "value": "2:3",
                  "x": "6953346.57466",
                  "y": "51227837.93128",
                },
                "_": "Mettmann, Neanderthal S",
              },
            ],
            "odvNameInput": "mettmann",
          },
          "itdOdvPlace": Object {
            "$": Object {
              "state": "empty",
            },
            "odvPlaceElem": "",
          },
        },
      },
    },
  },
}
  • You parsed the JSON string into an object. `console.log`ging an object doesn’t log it as JSON. There was probably no need to stringify+parse it at all, with `stations = result;` and `return stations;`. – Ry- Aug 10 '19 at 14:19
  • 1
    It looks like the "Object" string is what makes it invalid – hakanostrom Aug 10 '19 at 14:21
  • @Ry- Thanks for your comment! I've tried it and the result is still the same... pretty weird –  Aug 10 '19 at 14:29
  • @hakanostrom yes you are right! But why is it there? –  Aug 10 '19 at 14:32
  • from first console.log result if i try parsing it returns error and if i tried json parse after stringify the first console.log result it worked – vijay kumar Aug 10 '19 at 14:54
  • `JSON.parse` parses JSON (which is a string based representation of data) and returns the parsed result which is in your case an Object. So the second log is perfectly fine and expected. – t.niese Aug 10 '19 at 15:35
  • Why do you expect that `console.log(JSON.parse(stations))` should log some kind of JSON? If it returns JSON again, then why do you need the JSON at all? – t.niese Aug 10 '19 at 16:03
  • JSON exists only in one form and that is a string and is used to transfer data (e.g. over the network) or to store data (e.g. in a file or db) . If you want to work with the data encoded as JSON in a string you have to parse it. The result of the parse is an object on which you can work. As you want to work on `result` it does not make sense to encode it a JSON (`stations = JSON.stringify(result)`) in the first place. Just write `stations = result` and later `return stations` and with none of the `JSON.stringify` or `JSON.parse` – t.niese Aug 10 '19 at 16:14
  • @t.niese Thanks for this! But when I delete the stringify and parse the result is still the same as before (second console.log). Here it says [link](https://www.npmjs.com/package/xml2js) "So you wanna some JSON? Just wrap the result object in a call to JSON.stringify like this JSON.stringify(result). You get a string containing the JSON representation of the parsed object that you can feed to JSON-hungry consumers." –  Aug 10 '19 at 16:22
  • If you want JSON then why do you call `JSON.parse` on it? Either you want to have JSON then use the result of `JSON. stringify` or not, that's it. You seem to have a complete misunderstanding about what JSON is and/or means. – t.niese Aug 10 '19 at 16:25
  • JSON **is** a string you can not work on the data that is encoded in that string, you need to parse it using `JSON.parse`, but the result you get is not JSON anymore, there is nothing like an editable JSON. – t.niese Aug 10 '19 at 16:28

1 Answers1

0

You are already converting to string as a parsestring function. But it's also converting to JSON.stringify().

You can try check this value

let stations = await axios.get(encodeURI(`XMLAPi`));
console.log(JSON.stringify(stations.data)) // What is value??
hong developer
  • 13,291
  • 4
  • 38
  • 68