JSON data with array structure - json

I am using Neo4j and ExtJS in my application.
One good thing is that both handle JSON with array structure.
Neo4j returns and ExtJS can consume JSON like this:
{
columns: ["name", "age"],
data: [
["Peter", 34],
["Mike", 52]
]
}
instead of:
[
{"name": "Peter", "age" 34},
{"name": "Mike", "age" 52},
]
However, from my application server's HTTP API I want people to be able to choose which one of these JSON structures to receive.
So they are both JSON which means the HTTP header should be "Accept": "application/json". But how should I allow them to pick either one of the structures? Should they set a header or a query param? What is best practice?

I think that a simple param in the request would be enough.

Related

converting one JSON structure into another JSON structure

The client side expects JSON string in the below format
descriptions": [
{
"lang": "string",
"size": "string",
"text": "string",
"type": "string"
}
],
, but the received JSON is a bit different - like below
"descriptions":{
"desc":[
{
"size":string,
"lang":"string",
"type":"string",
"content":"string"
}
]
},
Is there anyway to ignore the "desc" part - for eg. using a JSON annotation ? Context: I am passing this JSON through a REST API call and it will be automatically converted to a Java object at the receiving end.
A simple
descriptions = descriptions.desc;
will do.
You just construct the object you need:
var clientDescriptions = descriptions.desc.map(function(d) {
size: d.size,
type: d.type,
lang: d.lang,
content: d.text
});
If you are using Gson, it is possible to use custom JsonDeserializer / JsonSerializer for your Java model. Your model object and API can then be cleanly implemented without having to deal with the different json structures.
In my personal experience, the backend has a bug and must be solved, it mustn't be sending malformed data.In the long run it is the best solution.

How to Retrieve and Query JSON type fields in Apache Solr 6.5

My Goal is to retrieve JSON type fields in an Solr index and also perform search queries on such fields.
I have the following documents in Solr Index and using the auto generated schema utilizing schemaless feature in Solr.
POST http://localhost:8983/solr/test1/update?commitWithin=1000
[
{"id" : "1", "type_s":"book", "title_t" : "The Way of Kings", "author_s" : "Brandon Sanderson",
"miscinfo": {"provider": "orielly", "site": "US"}
},
{"id" : "2", "type_s":"book", "title_t" : "The Game of Thrones", "author_s" : "James Sanderson",
"miscinfo": {"provider": "pacman", "site": "US"}
}
]
I see the JSON types are stored as strings in the schemaField type as seen in the output for following
GET http://localhost:8983/solr/test1/schema/fields
{
"name":"miscinfo",
"type":"strings"}
I had tried using srcField as mentioned in this post. However, a query to retrieve json type returns empty response. Below are the GET request used for the same
GET http://localhost:8983/solr/test1/select?q=1&fl=miscinfo&wt=json
GET http://localhost:8983/solr/test1/select?q=1&fl=miscinfo,source_s:[json]&wt=json
Also, the search queries for values inside JSON type fields return empty response
http://localhost:8983/solr/test1/select?q=pacman&wt=json
{
"responseHeader": {
"status": 0,
"QTime": 0,
"params": {
"q": "pacman",
"json": "",
"wt": "json"
}
},
"response": {
"numFound": 0,
"start": 0,
"docs": []
}
}
Please help in searching object types in Solr.
Have you checked this: https://cwiki.apache.org/confluence/display/solr/Response+Writers
JSON Response Writer A very commonly used Response Writer is the
JsonResponseWriter, which formats output in JavaScript Object Notation
(JSON), a lightweight data interchange format specified in specified
in RFC 4627. Setting the wt parameter to json invokes this Response
Writer. Here is a sample response for a simple query like
q=id:VS1GB400C3&wt=json:

REST API design: complex query with GET

I am designing a REST call that should deliver information for a location (lat/lon) and consider the user context/configuration.
As the number of user properties is high and nested, I am not sure, what is the correct way to design a new query (GET vs POST). Currently we use a POST request for simplicity - the query payload could look like this, but is custom and very different for each user. It also includes an array of multiple configuration items. Currently the request looks like this:
POST http://api.something.com/locationInformation
{
"location": {
"accuracy": 30,
"coordinates": [
16.34879820048809,
48.230067741347334
],
"provider": "network",
"timestamp": "2016-01-06T12:00:00.000Z"
},
"userConfiguration": [
{
"id": "asdfasdfasdfs09898sdf",
"values": [
"false"
]
},
{
"id": "iojkljio230909sdjklsdf",
"values": [
"99jkjiouio89",
"sdfilkjöjfoi093s09sdf"
]
}
]
}
So my question is: is it in such a case ok to "abuse" a POST request in order to query information?
Is there an elegant way to pass such data using a GET request?
Yes u can pass this data using a GET request by passing it to a request header.
use the header() method.Initialize a String variable say String data=//your json; and pass it to the header as follows header("data",data) in your client while building the request.

How to get the full pointer data in Parse.com

i'm making a new application using Parse.com as a backend, i'm trying to make less requests to the Parse, I have a class which is pointing to another object of another class.
Class1(things):
ObjectID Name Category(pointer)
JDFHSJFxv Apple QSGKqf343
Class2(Categories):
ObjectID Name Number Image
QSGKqf343 Fruits 45 http://myserver.com/fruits.jpeg
when i'm trying to retreive data for my first class things using REST API i'm getting this json object :
{
"results": [
{
"Name": "Apple",
"createdAt": "2015-07-12T02:50:20.291Z",
"objectId": "JDFHSJFxv",
"category": {
"__type": "Pointer",
"className": "Teams",
"objectId": "QSGKqf343"
},
"updatedAt": "2015-07-12T02:55:33.696Z"
}
]
}
the json doesn't contains all the data included in the object i'm pointing to, I will have to make another request to get all the data of that object,
is There any way to fix that
You need to tell Parse to return the related object in your query, via the include key.
e.g., add the following to your CURL --data-urlencode 'include=category'

Best way to structure json data with backbone

I'm just about to write a web app using backbone and want to know what's the best way to structure my json file? I've read that using 'dictionary' arrays are best but was wondering if there's a better way of structuring the data. An example of how the data should be structure would be great too.
My data seems to have a lot of nested arrays and these seem to be hard to search through.
JSON has two types of relevant container objects.
Object and Array.
Object is probably what you're thinking of when you say "dictionary array".
You probably want an object with arrays of objects :)
{"courses": [{
"name": "Spanish 101",
"subject": "How to speak Spanish",
}, {
"name": "Introduction to Film",
"subject": "Make pretty films",
}, {
"name": "Social Psychology",
"subject": "Why people are weird.",
}],
"sections": [],
"modules": [],
"topics": [],
"lessons": [],
// etc..
}
Each of the [] items would be field with numerous objects.
Once you get this data into your APP (either JSONP, AJAX, or if it's just assinged to a variable in our page) you can put them in your Backbone collections using the reset function (See: http://backbonejs.org/#Collection-reset):
var Courses = new Backbone.Collection;
function processData(data) {
Courses.reset(data.courses);
// etc...
}