How can I print a circular structure in a JSON-like format?

In Node.js, you can use util.inspect(object). It automatically replaces circular links with “[Circular]”. Albeit being built-in (no installation is required), you must import it import * as util from ‘util’ // has no default export import { inspect } from ‘util’ // or directly // or var util = require(‘util’) To use it, simply call … Read more

Is there any standard for JSON API response format?

Yes there are a couple of standards (albeit some liberties on the definition of standard) that have emerged: JSON API – JSON API covers creating and updating resources as well, not just responses. JSend – Simple and probably what you are already doing. OData JSON Protocol – Very complicated. HAL – Like OData but aiming … Read more

Jackson with JSON: Unrecognized field, not marked as ignorable

You can use Jackson’s class-level annotation: import com.fasterxml.jackson.annotation.JsonIgnoreProperties @JsonIgnoreProperties class { … } It will ignore every property you haven’t defined in your POJO. Very useful when you are just looking for a couple of properties in the JSON and don’t want to write the whole mapping. More info at Jackson’s website. If you want to … Read more

What is the difference between YAML and JSON?

Technically YAML is a superset of JSON. This means that, in theory at least, a YAML parser can understand JSON, but not necessarily the other way around. See the official specs, in the section entitled “YAML: Relation to JSON”. In general, there are certain things I like about YAML that are not available in JSON. … Read more

Ajax request returns 200 OK, but an error event is fired instead of success

jQuery.ajax attempts to convert the response body depending on the specified dataType parameter or the Content-Type header sent by the server. If the conversion fails (e.g. if the JSON/XML is invalid), the error callback is fired. Your AJAX code contains: dataType: “json” In this case jQuery: Evaluates the response as JSON and returns a JavaScript … Read more

Fetch: POST JSON data

With ES2017 async/await support, this is how to POST a JSON payload: (async () => { const rawResponse = await fetch(‘https://httpbin.org/post’, { method: ‘POST’, headers: { ‘Accept’: ‘application/json’, ‘Content-Type’: ‘application/json’ }, body: JSON.stringify({a: 1, b: ‘Textual content’}) }); const content = await rawResponse.json(); console.log(content); })(); Can’t use ES2017? See @vp_art’s answer using promises The question … Read more

Are multi-line strings allowed in JSON?

JSON does not allow real line-breaks. You need to replace all the line breaks with \n. eg: “first line second line” can saved with: “first line\nsecond line” Note: for Python, this should be written as: “first line\\nsecond line” where \\ is for escaping the backslash, otherwise python will treat \n as the control character “new … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)