30,426
questions
303
votes
2
answers
490k
views
How to extract and access data from JSON with PHP?
This is intended to be a general reference question and answer covering many of the never-ending "How do I access data in my JSON?" questions. It is here to handle the broad basics of decoding JSON in ...
1339
votes
37
answers
2.5m
views
How to parse JSON in Java
I have the following JSON text. How can I parse it to get the values of pageName, pagePic, post_id, etc.?
{
"pageInfo": {
"pageName": "abc",
"pagePic":...
185
votes
3
answers
583k
views
How do I parse JSON in Android? [duplicate]
How do I parse a JSON feed in Android?
1761
votes
16
answers
2.0m
views
Parse JSON in JavaScript? [duplicate]
I want to parse a JSON string in JavaScript. The response is something like
var response = '{"result":true,"count":1}';
How can I get the values result and count from this?
1550
votes
56
answers
1.8m
views
How to compare arrays in JavaScript?
I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to work.
var a1 = [1,2,...
251
votes
10
answers
80k
views
What is the difference between JSON and Object Literal Notation?
Can someone tell me what is the main difference between a JavaScript object defined by using Object Literal Notation and JSON object?
According to a JavaScript book it says this is an object defined ...
2478
votes
10
answers
612k
views
What is JSONP, and why was it created?
I understand JSON, but not JSONP. Wikipedia's document on JSON is (was) the top search result for JSONP. It says this:
JSONP or "JSON with padding" is a JSON extension wherein a prefix is specified ...
566
votes
19
answers
1.5m
views
How can I deserialize JSON with C#?
I have the following code:
var user = (Dictionary<string, object>)serializer.DeserializeObject(responsecontent);
The input in responsecontent is JSON, but it is not properly deserialized into ...
491
votes
16
answers
997k
views
How to loop through PHP object with dynamic keys [duplicate]
I tried to parse a JSON file using PHP. But I am stuck now.
This is the content of my JSON file:
{
"John": {
"status":"Wait"
},
"Jennifer": {...
1766
votes
58
answers
1.1m
views
Convert form data to JavaScript object with jQuery
How do I convert all elements of my form to a JavaScript object?
I'd like to have some way of automatically building a JavaScript object from my form, without having to loop over each element. I do ...
146
votes
11
answers
291k
views
Serialize and Deserialize Json and Json Array in Unity
I have a list of items send from a PHP file to unity using WWW.
The WWW.text looks like:
[
{
"playerId": "1",
"playerLoc": "Powai"
},
{
"playerId": "2",
"...
1504
votes
28
answers
1.5m
views
Safely turning a JSON string into an object
Given a string of JSON data, how can I safely turn that string into a JavaScript object?
Obviously I can do this unsafely with something like:
var obj = eval("(" + json + ')');
but that leaves me ...
432
votes
14
answers
676k
views
Receive JSON POST with PHP [duplicate]
I’m trying to receive a JSON POST on a payment interface website, but I can’t decode it.
When I print :
echo $_POST;
I get:
Array
I get nothing when I try this:
if ( $_POST ) {
foreach ( $...
1288
votes
47
answers
1.8m
views
Parsing JSON with Unix tools
I'm trying to parse JSON returned from a curl request, like so:
curl 'http://twitter.com/users/username.json' |
sed -e 's/[{}]/''/g' |
awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; ...
1192
votes
31
answers
1.2m
views
Deserialize JSON into C# dynamic object? [duplicate]
Is there a way to deserialize JSON content into a C# dynamic type? It would be nice to skip creating a bunch of classes in order to use the DataContractJsonSerializer.
493
votes
15
answers
826k
views
jQuery AJAX cross domain
Here are two pages, test.php and testserver.php.
test.php
<script src="scripts/jq.js" type="text/javascript"></script>
<script>
$(function() {
$.ajax({...
152
votes
9
answers
63k
views
How to handle both a single item and an array for the same property using JSON.net
I'm trying to fix my SendGridPlus library to deal with SendGrid events, but I'm having some trouble with the inconsistent treatment of categories in the API.
In the following example payload taken ...
11595
votes
39
answers
3.9m
views
Which JSON content type do I use?
There are many "standards" for the JSON content type:
application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json
Which one do I use, and where? I assume ...
415
votes
6
answers
716k
views
How can I parse (read) and use JSON in Python?
My Python program receives JSON data, and I need to get bits of information out of it. How can I parse the data and use the result? I think I need to use json.loads for this task, but I can't ...
1446
votes
46
answers
1.6m
views
How to make a class JSON serializable
How to make a Python class serializable?
class FileItem:
def __init__(self, fname):
self.fname = fname
Attempt to serialize to JSON:
>>> import json
>>> x = FileItem('/...
2162
votes
42
answers
811k
views
How do I format a Microsoft JSON date?
I'm taking my first crack at Ajax with jQuery. I'm getting my data onto my page, but I'm having some trouble with the JSON data that is returned for Date data types. Basically, I'm getting a string ...
396
votes
13
answers
357k
views
Split / Explode a column of dictionaries into separate columns with pandas
I have data saved in a postgreSQL database. I am querying this data using Python2.7 and turning it into a Pandas DataFrame. However, the last column of this dataframe has a dictionary of values inside ...
48
votes
3
answers
7k
views
How can I parse a JSON string that would cause illegal C# identifiers?
I have been using NewtonSoft JSON Convert library to parse and convert JSON string to C# objects. But now I have came across a really awkward JSON string and I am unable to convert it into C# object ...
686
votes
15
answers
921k
views
Return JSON response from Flask view
I have a function that analyzes a CSV file with Pandas and produces a dict with summary information. I want to return the results as a response from a Flask view. How do I return a JSON response?
@...
511
votes
13
answers
890k
views
How to get POSTed JSON in Flask?
I'm trying to build a simple API using Flask, in which I now want to read some POSTed JSON. I do the POST with the Postman Chrome extension, and the JSON I POST is simply {"text":"lalala"}. I try to ...
340
votes
9
answers
388k
views
How to implement custom JsonConverter in JSON.NET?
I am trying to extend the JSON.net example given here
http://james.newtonking.com/projects/json/help/CustomCreationConverter.html
I have another sub class deriving from base class/Interface
public ...
1149
votes
20
answers
1.6m
views
Returning JSON from a PHP Script
I want to return JSON from a PHP script.
Do I just echo the result? Do I have to set the Content-Type header?
65
votes
7
answers
82k
views
How to add both file and JSON body in a FastAPI POST request?
Specifically, I want the below example to work:
from typing import List
from pydantic import BaseModel
from fastapi import FastAPI, UploadFile, File
app = FastAPI()
class DataConfiguration(BaseModel)...
245
votes
10
answers
802k
views
Deserialize JSON with C#
I'm trying to deserialize a Facebook friend's Graph API call into a list of objects. The JSON object looks like:
{"data":[{"id":"518523721","name":"ftyft"},
{"id":"527032438","name":"...
1503
votes
3
answers
2.7m
views
Why can't Python parse this JSON data? [closed]
I have this JSON in a file:
{
"maps": [
{
"id": "blabla",
"iscategorical": "0"
},
{
&...
302
votes
15
answers
577k
views
Converting JSON data to Java object
I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...
557
votes
29
answers
464k
views
Infinite Recursion with Jackson JSON and Hibernate JPA issue
When trying to convert a JPA object that has a bi-directional association into JSON, I keep getting
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError)
All I found ...
3484
votes
32
answers
1.9m
views
pretty-print JSON using JavaScript
How can I display JSON in an easy-to-read (for human readers) format? I'm looking primarily for indentation and whitespace, with perhaps even colors / font-styles / etc.
870
votes
22
answers
1.0m
views
How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?
I have a simple key/value list in JSON being sent back to ASP.NET via POST. Example:
{ "key1": "value1", "key2": "value2"}
I AM NOT TRYING TO DESERIALIZE INTO STRONGLY-TYPED .NET OBJECTS
I simply ...
1103
votes
10
answers
1.8m
views
How to POST JSON data with Python Requests?
I need to POST a JSON from a client to a server. I'm using Python 2.7.1 and simplejson. The client is using Requests. The server is CherryPy. I can GET a hard-coded JSON from the server (code not ...
154
votes
3
answers
178k
views
How to auto-generate a C# class file from a JSON string [closed]
Given the following JSON object,
form = {
"name": "",
"address": {
"street": "",
"city": "",
"province": "",
"postalCode": "",
"country": ""
},
"phoneDay": "",
"...
736
votes
30
answers
803k
views
Can I set an unlimited length for maxJsonLength in web.config?
I am using the autocomplete feature of jQuery. When I try to retrieve the list of more then 17000 records (each won't have more than 10 char length), it's exceeding the length and throws the error:
...
1233
votes
11
answers
931k
views
Serializing to JSON in jQuery [duplicate]
I need to serialize an object to JSON. I'm using jQuery. Is there a "standard" way to do this?
My specific situation: I have an array defined as shown below:
var countries = new Array();
countries[0]...
1822
votes
17
answers
2.9m
views
How do I write JSON data to a file?
How do I write JSON data stored in the dictionary data to a file?
f = open('data.json', 'wb')
f.write(data)
This gives the error:
TypeError: must be string or buffer, not dict
1554
votes
16
answers
2.0m
views
What is the "right" JSON date format?
I've seen so many different standards for the JSON date format:
"\"\\/Date(1335205592410)\\/\"" .NET JavaScriptSerializer
"\"\\/Date(1335205592410-0500)\\/\"" .NET ...
931
votes
13
answers
763k
views
Saving UTF-8 texts with json.dumps as UTF-8, not as a \u escape sequence
Sample code (in a REPL):
import json
json_string = json.dumps("ברי צקלה")
print(json_string)
Output:
"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"
The problem: it's not human ...
330
votes
15
answers
613k
views
JSON encode MySQL results
How do I use the json_encode() function with MySQL query results? Do I need to iterate through the rows or can I just apply it to the entire results object?
566
votes
15
answers
598k
views
What is JSON and what is it used for?
I've looked on Wikipedia, googled it, and read the official documentation, but I still haven't got to the point where I really understand what JSON is, and why I'd use it.
I have been building ...
125
votes
11
answers
139k
views
Correctly Parsing JSON in Swift 3
I'm trying to fetch a JSON response and store the results in a variable. I've had versions of this code work in previous releases of Swift, until the GM version of Xcode 8 was released. I had a look ...
320
votes
26
answers
948k
views
How can I convert JSON to CSV?
I have a JSON file I want to convert to a CSV file. How can I do this with Python?
I tried:
import json
import csv
f = open('data.json')
data = json.load(f)
f.close()
f = open('data.csv')
csv_file = ...
1115
votes
19
answers
1.8m
views
Fetch: POST JSON data
I'm trying to POST a JSON object using fetch.
From what I can understand, I need to attach a stringified object to the body of the request, e.g.:
fetch("/echo/json/",
{
headers: {
'...
305
votes
12
answers
198k
views
Does JSON syntax allow duplicate keys in an object?
Is this valid json?
{
"a" : "x",
"a" : "y"
}
http://jsonlint.com/ says yes.
http://www.json.org/ doesn't say anything about it being forbidden.
But ...
272
votes
24
answers
204k
views
ASP.NET MVC JsonResult Date Format
I have a controller action that effectively simply returns a JsonResult of my model. So, in my method I have something like the following:
return new JsonResult(myModel);
This works well, except for ...
19
votes
2
answers
65k
views
Why does Gson fromJson throw a JsonSyntaxException: Expected BEGIN_OBJECT but was BEGIN_ARRAY?
(This post is meant to be a canonical question with a sample answer provided below.)
I'm trying to deserialize some JSON content into a custom POJO type with Gson#fromJson(String, Class).
This piece ...
1261
votes
15
answers
2.2m
views
How do I turn a C# object into a JSON string in .NET?
I have classes like these:
class MyDate
{
int year, month, day;
}
class Lad
{
string firstName;
string lastName;
MyDate dateOfBirth;
}
And I would like to turn a Lad object into a ...