Handle invalid Json responce from 3rd Paty

Asked
Active3 hr before
Viewed126 times

6 Answers

invalidhandle
90%

Meta Stack Overflow , Stack Overflow Public questions & answers , Stack Overflow help chat , Thanks for the edit, but the console logging needs to happen inside of your getDetails() function. Logging afterwards is already too late, because it's trying to return JSON. As I mentioned in my first comment, you need to inspect the value of response to see if response.json() is failing. – John H Sep 28 '20 at 21:05

Backend Code:

import {
   fetch
} from 'wix-fetch';
import {
   wixData
} from 'wix-data';

export function getDetails(address, citystatezip) {
   const url = 'https://api.gateway.attomdata.com/propertyapi/v1.0.0/property/expandedprofile?address1=' + address + "&address2=" + citystatezip;
   let headers = {
      "apikey": "xxxxxxxx",
      "accept": "application/json"
   };
   let options = {
      headers: headers
   }

   console.log("Url: " + url);

   return fetch(url, {
         method: 'get'
      })
      .then(response => {
         return response.json();
      })
      .then((data) => {
         console.log(data);
         return data;
      });
}
load more v
88%

Clicking on it will show you more details which may give you some clues about which plugin or third-party service may be causing the issue. ,You can now visit your website and edit and post or page to see if you can reproduce the invalid JSON response error. ,Are you trying to fix the invalid JSON error in WordPress? ,The invalid JSON response error would also mean that WordPress REST API on your website encountered an error.

Once the file opens, you need to delete all the code inside it and replace it with the following code:

# BEGIN WordPress

RewriteEngine On
RewriteRule.*-[E = HTTP_AUTHORIZATION: % {
   HTTP: Authorization
}]
RewriteBase /
   RewriteRule ^ index\.php$ - [L]
RewriteCond % {
   REQUEST_FILENAME
}!-f
RewriteCond % {
   REQUEST_FILENAME
}!-d
RewriteRule. / index.php[L]

# END WordPress
load more v
72%

The error message is caused because WorsPress is returning HTML before/after the JSON, which is most likely an error message. So going to that endpoint manually should give you the reason.,As for your second question; WordPress handles menu’s as a (custom) post type, which is normal since it’s not an actual page or post. In the thread you mentioned the problem was caused by a CPT called menu’s, at least as far as I can tell.,However I tried that endpoint and it only returns an empty array (which makes sense, since your homepage is not a post). So I guess the error has been resolved and should work again. It has nothing to do with the menu handler or head tags though, at least I haven’t seen any problems with it in my projects.,I found this thread which shows the person had the same issue I believe. So I tried to implement solution in the menu-handler and it doesn’t seem to make any difference. This is what it looks like in my menu-handler:

I’m currently having this issue that the json response body is invalid:

FetchError: invalid json response body at http://hippiekick.byethost3.com/wp-json/wp/v2/posts?_embed=true&slug=startsida reason: Unexpected token < in JSON at position 0 at eval (webpack-internal:///./node_modules/node-fetch/lib/index.mjs:276:32) at eval (webpack-internal:///./node_modules/@frontity/react-easy-state/dist/bundle.js:58:245) at batchedUpdates$1 (webpack-internal:///./node_modules/react-dom/cjs/react-dom.development.js:3767:145) at batch (webpack-internal:///./node_modules/@frontity/react-easy-state/dist/bundle.js:58:237) at Object.apply (webpack-internal:///./node_modules/@frontity/react-easy-state/dist/bundle.js:61:179) at runMicrotasks (<anonymous>)
   at processTicksAndRejections (internal/process/task_queues.js:97:5)
   at Object.populate (webpack-internal:///./node_modules/@frontity/wp-source/src/libraries/populate.ts:27:12)
   at Object.eval (webpack-internal:///./node_modules/@frontity/wp-source/src/libraries/handlers/postType.ts:35:217)
   at eval (webpack-internal:///./node_modules/@frontity/wp-source/src/actions.ts:40:1)
load more v
65%

In this article you will learn what JSON is and how you can deal with errors occurring when parsing JSON data, such as "Unexpected Token < in JSON at Position 0.",In the old days XML was primarily used for interchanging data, but since JSON appeared it is often used as a replacement of XML. A JSON file should end with the .json extension., JSON Web Tokens are a useful tool and a better way of implementing authorization in web applications, but what exactly are they and how do they work? ,JSON is an extremely popular format and is commonly used for data interchanging. We have talked about what JSON is, and what we can do when a parsing error is thrown. For starters, always double-check if the values you are dealing with are what you expect them to be, and remember, console.log() is your friend.

Below you can see an example of a JSON format.

{
   "name": "animals",
   "animals": [
      "Dog",
      "Cat",
      "Mouse",
      "Pig",
      "Bear"
   ],
   "hobbies": {
      "football": false,
      "reading": true
   }
}
load more v
75%

it means exactly what it says. DataTables made an Ajax response to the server for JSON data, but the data that it received back was not valid JSON.,Once you have followed the instructions above to find out what the data from the server actually is, since it is not JSON, you will be able to modify the data, or the program that creates the data, to return valid JSON only.,Click the Response tab in the Ajax view to see the exact data that was returned from the server. This is what we are interested in!,Click the Reponse tab in the Ajax view to see the exact data that was returned from the server. This is what we are interested in!

load more v
40%

To fix the issue with the sample JSON to XML policy, pass a valid JSON payload. For example:,To fix the issue with the sample JSON to XML policy discussed above, pass a valid JSON payload request as follows:,Ensure that the input passed to the JSON to XML policy in the <Source> element is valid JSON payload and non empty.,For example, if the following invalid JSON is provided as input to JSON to XML policy,

Identify the JSON to XML policy where the error occurred and the name of the variable that is not available. You can find both of these items in the faultstring element of the error response. For example, in the following faultstring, the policy name is Convert-JSONToXML and the variable is response:

"faultstring": "JSONToXML[Convert-JSONToXML]: Source response is not available"
load more v

Other "invalid-handle" queries related to "Handle invalid Json responce from 3rd Paty"