I am trying to output the content i get from my Controller in my view as json, but i think i is outputting weird.
On the web i search json and it comes up with output looking like this:
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
However mine is just not formatted and looks like this.
[{"Customer":{"id":"1","first_name":"Ian","last_name":"Smith","address_1":"10 High Streets","address_2":"","town_city":"Plymouth","county":"Devon","postcode":"PL1 2JD"}},{"Customer":{"id":"2","first_name":"David","last_name":"Smith","address_1":"52 Low Avenue","address_2":"","town_city":"Exeter","county":"Devon","postcode":"EX2 1KO"}}]
How can i output it so it looks like the first one?
EDIT
Controller
$user = $this->Customer->find( 'all' );
$this->set( 'users', $user );
View
<?php echo json_encode($user); ?>
There is no sense to beautify your json on output step. If it matters, you may use external tools to make a pretty look of json.
Also, consider using (JSON View) in Cake.
In short, you set a special view variable with content you want to jsonify:
for local effect, write in your action Router::parseExtensions()
specify variable which contains your data to be output $this->set('_serialize', array('response')); (in json, there will be a root object called "response" with content of $response variable).
With such approach, you won't need to create view files - json will be output automatically if request has "Accept: application/json" header.
Only difference in those json responses are that the first one is JSON object with sub objects and second one is an array of JSON objects with their sub objects.
The code below retrieves all records for customers. And when you encode it into json object, it is encoded as array of Cutomers
$this->Customer->find( 'all' );
You can achieve response like the first one by
$this->Customer->find( 'first' );
The above code will only yield one Customer object.
Related
I am getting data in this format how to fetch it and use in UI?
Currently i know to ways of fetching data by using snapshot and another one is by storing whole response object is list of array and then fetching data by its key name but there is no key-value pair format in this.So how to fetch data?
[["2022-10-20","baner","03:15:00","no",3000,"Online",147,338,"Owner","Open","8993333333","12000","Adhiraj Nivas ",22019991811063,"pune","Sumitra","Vaishanvi"],["2022-10-19","baner","03:15:00","no",13000,"Online",161,327,"Owner","Open","7652222222","30000","Yashoda Recidency",22100199963254,"pune","sonali Jadhav","Satish"]]
The data is actually a list of lists. To reference a specific element in this data, you need to use two indexes. Please see example code below:
void main() {
final listsData = [
["2022-10-20",
"baner",
"03:15:00",
"no",
3000,
"Online",
147,
338,
"Owner",
"Open",
"8993333333",
"12000",
"Adhiraj Nivas ",
22019991811063,
"pune",
"Sumitra",
"Vaishanvi"
],
[
"2022-10-19",
"baner",
"03:15:00",
"no",
13000,
"Online",
161,
327,
"Owner",
"Open",
"7652222222",
"30000",
"Yashoda Recidency",
22100199963254,
"pune",
"sonali Jadhav",
"Satish"
]
];
print(listsData[0][1]); // prints 'baner'
print(listsData[1][12]); // prints 'Yashoda Recidency'
}
This looks like a List
In dart the equivalent would be a List<dynamic> and you would traverse it with a for loop or specific indices.
It's simply an object. you can do it by using "List" concept with a custom model class.
I'm building a logic app that pulls some JSON data from a REST API, parses it with the Parse JSON block, and pushes it to Azure Log Analytics. The main problem I'm hitting is an important JSON field can either be an object or null. Per this post I changed the relevant part of my JSON schema to something like this
"entity": {"type": ["object", "null"] }
While this works, I'm now no longer to access entity later in the logic app as dynamic content. I can access all other fields parsed by the Parse JSON block downstream in the logic (that don't have nullable field). If I remove the "null" option and just have the type set to object, I can access entity in dynamic content once again. Does anyone know why this might be happening and/or how to access the entity field downstream?
Through the test, if we use "entity": {"type": ["object", "null"] }, we really cannot directly select entity in dynamic content.
But we can use the following expression to get the entity:
body('Parse_JSON')?['entity']
The test results seem to be no problem:
For a better understanding, let me cite a few more examples:
1. If your json is like this:
{
"entity": {
"testKey": "testValue"
}
}
Your expression is like this:
body('Parse_JSON')?['entity']
2. If your json is like this:
{
"test": {
"entity": {
"testKey": "testValue"
}
}
}
Your expression should like this:
body('Parse_JSON')?['test']?['entity']
I am POSTing valid JSON to a logic app and the request body is a simple set of name/value pairs in JSON format. I use parse JSON action to turn the JSON into a variable. (I am not sure if this is absolutely required, or if I can reference the http body directly without a JSON object).
[
{
"name": "fullname",
"value": "joe schmoe"
},
{
"name": "email",
"value": "joeschmoe#acme.com"
}
]
All I need to do (and this is driving me nuts) is create two variables, one containing the value of the email field and one containing the value of the fullname field.
As soon as I try to use the output value value, the logic app replaces my action with a For Each action, and then I try to to assign item().value[0] or item().value[1] to a variable without luck.
I have read dozens of examples online, but of course they all seem to be parsing JSON where there is largely unique elements in the name:value pairs.
While this is a bit of a newb question, I'm confused and need advice.
Thank you.
I used a parseJSON action to ensure I have a varaible containing the JSON array.
I then referenced the array value with (explained):
"from the body of the output from the Parse JSON action, refernce the first record in the set (fullname) and then the value of that record, the value 'joe schmoe'"
#{body('Parse_JSON')[0]['value']} (returns fullname)
Email is similar, just 2nd record in the collection:
#{body('Parse_JSON')[1]['value']} (returns email address)
In my Flask server I am receiving a JSON-encoded parameter which is being sent via an HTTP POST from the client application.
Here is an example of what the JSON object looks like. For simplicity, I have kept only the first 2 entries, but the full object contains many more such entries.
[
{
"id": 1,
"start": 7.85,
"end": 9.813,
"text": "Θέλω να",
"words": [
"Θέλω",
"να"
],
"isBeingEditedByUser": false,
"translatedText": "I want to"
},
{
"id": 2,
"start": 9.898,
"end": 13.055,
"text": "Από κάτι το πήραν πολύ άσχημα ο οπαδός του Ολυμπιακού",
"words": [
"Από",
"κάτι",
"το",
"πήραν",
"πολύ",
"άσχημα",
"ο",
"οπαδός",
"του",
"Ολυμπιακού"
],
"isBeingEditedByUser": false,
"translatedText": "Something very bad for Olympiacos fan"
}
]
My understanding is that this JSON structure corresponds to an Array (in Javascript) or a List in Python. In this case, it is an array containing two elements, where each element is itself an object.
However, when I try to use the object on the Flask side, it seems that it has been mapped to a string (rather than a List). Is this normal behavior? I have not been able to find any documentation which states that this is the normal mapping. I would have expected the JSON object to be mapped to a Python List object instead, but this is not happening.
I know that I can use python.loads() myself to convert the string into the appropriate List structure, but I did not expect to have to do this and want to make sure that I am not misunderstanding something here.
Here is a snippet of code which shows the relevant portion in my Flask function:
#app.route('/update_SRT_file', methods=['POST'])
def update_SRT_file():
# Validate the request body contains JSON
if request.is_json:
json_obj = request.get_json()
eprint("update_SRT_file: received JSON object: ")
eprint("Type of received object is", type(json_obj));
else:
eprint("update_SRT_file: Request was not JSON ")
Here is what gets printed out:
23:12:12.771824 update_SRT_file: received JSON object:
23:12:12.771878 Type of received object is **<class 'str'>**
After more investigation, the problem was occuring because the client was JSON encoding the data twice. Upcon removing the additional encoding, it was found that now Flask correctly maps the incoming JSON Array to a python List structure.
Your POST request is sending the JSON as raw text. Flask then receives it as raw text in the request body. Some web application frameworks might automatically parse the text into a JSON-like object or data structure, but Flask does not, at least not out of the box.
I'm storing addresses data in Couchdb, and am looking for a way to get an array of just the values, instead of key: value for every record.
This is the current response:
{"total rows": 2438, "offset": 0, "rows":[
{"id": "ec5de6de2cf7bcac9a2a2a76de5738e4", "key": "user_298774", "value": {"city": "Milano", "address":"Corso Como, 42b"},
{"id": "a2a2a76de573ae4ec5de6de2cf7bcac9", "key": "user_276341", "value": {"city": "Vicenza", "address":"Via Quinto Sella, 118"}
... (etc).
]}
I only really need:
[{"city": "Milano", "address":"Corso Como, 42b"},
{"city": "Vicenza", "address":"Via Quinto Sella, 118"},
...]
I really need to minimize the usage of bandwidth that a JSON response consumes. I can't seem to find a way to transform the view into a simple array. Suggestions?
The response you are getting conforms to the Couch's REST based protocol. To reformat it two methods are provided: show functions and list functions. Basic idea is the same, but the first is suitable for retrieval documents and the list function is for you!
The list function runs the query inside the server and send the output arbitrary transformed with your JS code. API you will need is simple:
Fetch each record from the view with the getRow() function.
Export to the string (containing JSON) your JS object obj with toJSON(obj).
Send the output to the client with send(json).
If the map/reduce view URL with data is /mydb/_design/myapp/_view/mydocs-by-user and the list function name is mylist get the reformatted result to the client with the URL /mydb/_design/myapp/_list/mylist/mydocs-by-user.
Please refer to the list function documentation cited above and the chapter in the Guide for the longed tutorial.