$scope.data = {
"statusCode": 200,
"message": "success",
"result": {
"data2": [
{
"viewTitle": "one",
"viewType": 1,
"viewData": "{\"data3\":{\"_id\":\"one\"," +
"\"channelId\":\"0\",\"channelName\":\"animals\"," +
"\"rate\":3.5,\"visited\":21,\"meta\":{\"currency\":\"t\"" +
",\"size\":\"5 mb\",\"updatedAt\":\"2 2 2016\"" +
",\"createdAt\":\"2 2 2016\",\"duration\":\"3 minutes \"}" +
",\"thumbnail\":\"127.0.0.1\"" +
",\"banner\":\"127.0.0.1\"" +
",\"trailerUrl\":\"\",\"url\":\"127.0.0.1\"" +
",\"desc\":\"my informations \"" +
",\"title\":\"my videos number 4\",\"packages\"" +
":[{\"_id\":\"1\",\"price\":500" +
",\"type\":\"purchase\"}],\"packageId\":\"2\"" +
",\"packagePrice\":500,\"userFavorite\":0,\"userRate\":0,\"locked\":1}}"
}
]
}
};`
I try to access for example chanelName or size please help to resolve.
first, turn the JSON string into an object via JSON.parse();
var data = JSON.parse();
//data is now the result of JSON.parse
and then access the property you want to use in $scope.data
I have created a CodePen for your Problem:
http://codepen.io/JohannesFerner/pen/PNmmBd/
You have JSON.parse(<escaped JSON property>) or angular.fromJson(<escaped JSON property>) (see the comment above) your escaped JSON.
Without further knowledge of your backend I would suggest that you do not deliver escaped JSON to the client.
First level objects can be accessed like this.
person.result.data2[0].viewTitle
JSON.parse is the easiest way here to disassemble the complex object. I have given my example below
http://jsfiddle.net/MisterFantastic/u8bhc1ds/1/
JSON.parse(person.result.data2[0].viewData)
For complex objects please refer this link.
AngularJS format JSON string output
Related
Example:
API A:
{
"customer":[
{
"name":"Jane",
"phone":"9999999",
"email":"jane#test.com"
},
{
"name":"John",
"phone":"8888888",
"email":"john#test.com"
},
{
"name":"Joe",
"phone":"7777777",
"email":"Joe#test.com"
}
]
}
Using the JSON extractor, I want to get the names of all the customers
so: Jane, John, Joe
How do I get these values and turn them into an array
[{"name":"Jane", "name":"John", "name":"Joe"}]
And pass it onto the next API?
Note: That it has to be dynamic so API A could show different 2 names or 1 name or more and needs to be adjusted into the array
First of all your [{"name":"Jane", "name":"John", "name":"Joe"}] is not a valid JSON, you can check it yourself:
so I strongly doubt that this is the string you need to generate.
So if you really need to construct this value you can do something like:
Add JSR223 PostProcessor as a child of the request which returns this "customers" data
Put the following code into "Script" area:
def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def payload = new StringBuilder()
payload.append('[{')
0.upto(response.customer.size - 1, { index ->
payload.append('"name": "').append(response.customer[index].name).append('"')
if (index != response.customer.size - 1) {
payload.append(',')
}
})
payload.append('}]')
vars.put('payload', payload as String)
Refer the generated value as ${payload} where required
Demo:
More information:
JsonSlurper
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
I want to extract below json and use values accordingly.
I/p JSON:-
{
"status": "Success",
"message": "User created successfully",
"id": [
131188,
131191
]
}
Here I want values of id field. I used JSON Extractor and gave expression as $.id which gives me [131188,131191] in a variable. Now I want to use individual values out of this array i.e. 131188 and 131191.
Any Idea how to do it?
Update : I don't want to use 2 JSON Extractors.
Just add [*] to your JSON path expression as below
$.id[*]
This will create a jmeter variable for each value.Note that you should use -1 in the match numbers field.
You could use a json extractor and a "JSR223 PostProcessor" with groovy language. An example:
import groovy.json.JsonSlurper
//String jsonString = vars.get("jsonFromExtractor")
String jsonString = '''
{
"status": "Success",
"message": "User created successfully",
"id": [
131188,
131191
]
}
'''
log.info("jsonString:" + jsonString)
def json = new JsonSlurper().parseText( jsonString )
String idValue1 = json.get("id").get(0)
String idValue2 = json.get("id").get(1)
log.info("idValue1:" + idValue1)
log.info("idValue2:" + idValue2)
I hope this helps
I have been following the instructions here:
https://www.codenameone.com/javadoc/com/codename1/io/JSONParser.html to retrieve a value from a json file.
I have managed to read the top level value of my json content - however I cannot see how to read the value of a nested tag e.g. using this file ...
{
"glossary":{
"title":"example glossary",
"GlossDiv":{
"title":"S",
"GlossList":{
"GlossEntry":{
"ID":"SGML",
"SortAs":"SGML",
"GlossTerm":"Standard Generalized Markup Language",
"Acronym":"SGML",
"Abbrev":"ISO 8879:1986",
"GlossDef":{
"para":"A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso":[
"GML",
"XML"
]
},
"GlossSee":"markup"
}
}
}
}
}
Please can someone show me how to get to the value of "para" above?
Thanks
After parsing your json data based on this, you can use Result to read deep into the json content:
import com.codename1.processing.Result;
...
Map<String, Object> data = json.parseJSON(r);
Result result = Result.fromContent(data);
String id = result.getAsString("glossary/GlossDiv/GlossList/GlossEntry/ID");
String para = result.getAsString("glossary/GlossDiv/GlossList/GlossEntry/GlossDef/para");
I am getting this req in my req.body . I want to parse the details .Below is my req.
"[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"
I want output like
number:
cmdb_ci:
category:
How do i parse this array object in nodejs. Please help
Use JSON.parse() like this:
var aJsonArrString = "[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"
var aObjList = JSON.parse(aJsonArrString);
for(var i = 0; i < aObjList.length; i++) {
console.log('number : ' + aObjList[i].number);
console.log('cmdb_ci : ' + aObjList[i].cmdb_ci);
console.log('category : ' + aObjList[i].category);
}
You Can Use
JSON.parse(req.body);
This looks like JSON, I don't know if the escaping \ are coming from your way of logging the value or something so I'll expect it is a valid string to start off.
You can use
var my_list = JSON.parse(req.body);
//Access like any other array...
my_list[0].number;
I'm working on AWS lambda + API Gateway, and I need to pass an array of numbers in the url (GET method) for a REST call. It seems a good way is to pass the numbers as string (comma separated) and then use JSON.parse for the conversion to an array of numbers.
Following is the AWS lambda code I'm using;
exports.handler = (event, context, callback) => {
var arr = JSON.parse('[' + event.numbers + ']');
console.log("array: " + arr);
// TODO implement
callback(null, 'Hello from Lambda');
};
I'm testing this function in AWS Lambda using this Input test event;
{
"numbers": "1,5"
}
And everything works as expected; no errors.
However, when I test it via API Gateway, and passing the numbers as string in the query, I get following error (observed via CloudWatch);
*19:19:02
START RequestId: eabab882-8cee-11e7-8e2f-79d3086e061f Version: $LATEST
19:19:02
2017-08-29T19:19:02.688Z eabab882-8cee-11e7-8e2f-79d3086e061f SyntaxError: Unexpected token u in JSON at position 1 at Object.parse (native) at exports.handler (/var/task/index.js:4:20)
19:19:02
END RequestId: eabab882-8cee-11e7-8e2f-79d3086e061f
19:19:02
REPORT RequestId: eabab882-8cee-11e7-8e2f-79d3086e061f Duration: 215.25 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 18 MB
19:19:02
RequestId: eabab882-8cee-11e7-8e2f-79d3086e061f Process exited before completing request*
This is the request passed to lambda as shown in the log;
"body-json" : {},
"params" : {
"path" : {
}
,"querystring" : {
"numbers" : "1,6"
}
,"header" : {
}
},
"stage-variables" : {
},
I can't figure out what the problem is, since I'm passing same string in both cases.
I would appreciate any help.
Thanks
Gus
With this input json informed, you need to get it like this:
var arr = JSON.parse('[' + event.params.querystring.numbers + ']');
rather than:
var arr = JSON.parse('[' + event.numbers + ']');
Or make a body mapping template to stay the way you want:
{ "number": "$input.params('number')" }
http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
I hope I have helped!