Andriy
Andriy

Reputation: 1610

Why doesn't Firefox native JSON viewer work for my request?

I am trying to see a response to my url in Firefox JSON viewer, but it is always shown as a plain text. I have option devtools.jsonview.enabled set to true. I send a request with a header Accept: application/json and get the response with the 'correct' content-type: application/json. Unfortunately, it does't work with my url.

However, Firefox JSON viewer works pretty well with another url. What is it wrong with this one?

Upvotes: 2

Views: 3766

Answers (4)

Rubie
Rubie

Reputation: 11

I had a similar issue. For me, it was simply that I had a valid JSON string but not a JSON object. Simply using the json.loads(data) function in my Python code did the trick for me. There should be a similar library for whatever language you’re using.

Upvotes: 1

KorkOoO
KorkOoO

Reputation: 612

You can use this userscript:

// ==UserScript==
// @name         Force JSON Viewer for Specific URLs
// @namespace    whatever
// @version      1.0
// @description  Force JSON rendering for specific URLs in Firefox
// @author       YourName
// @match        https://auction-sandbox.ea.openprocurement.org/database/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Get the current URL
    const url = window.location.href;

    // Fetch the content, force JSON rendering
    fetch(url)
        .then(response => response.json())
        .then(json => {
            const jsonString = JSON.stringify(json, null, 2);
            const blob = new Blob([jsonString], { type: 'application/json' });
            const objectURL = URL.createObjectURL(blob);
            window.location.href = objectURL;
        })
        .catch(error => console.error('Error fetching or parsing JSON:', error));
})();

it's permeant solution IMHO.

Upvotes: 0

PhoenixWright
PhoenixWright

Reputation: 1

I too have this issue and don't have access to change the server.

If you are just trying to view your JSON, like me, try Postman or another browser. My results below.

Firefox developers edition: JSON values as string.

Chrome and Edge: XML.

IE: Downloads JSON

Upvotes: 0

NVRM
NVRM

Reputation: 13087

After some investigations following your example link, and as you said, the json is valid and well formated.

But the server does not send the application/json header correctly.

If you can't modify the server, you can still proxy it with the correct header from another server, like this:

Example in php:

<?php

header('Content-Type: application/json');
$data = file_get_contents("https://auction-sandbox.ea.openprocurement.org/database/11111111111111111111111111110149");
echo $data;

Output:

enter image description here

Upvotes: 3

Related Questions