How to parse a json from URL? - json

I have a JSON URL I need to parse, How can we do that?

all the info you need on how to parse json is described here:
http://www.json.org/

http://developer.android.com/reference/org/json/JSONObject.html
For an object:
JSONObject json = new JSONObject(json_string);
For an array:
JSONArray json = new JSONArray(json_string);

Related

How to replace a JSON value stored in a JSON file and use that in a Rest Assured test

I have a set of input data files in JSON and I am trying to replace a value present in a JSON file and use that value to do a post request in restAssured
The JSON file has
{
"items": [
{
"item_ref": 241,
"price": 100
}
]
}
jsonbody below is a String of the above JSON file
This is the code that fails:
JSONObject jObject = new JSONObject(jsonbody);
jObject.remove("item_ref");
jObject.put("item_ref","251");
System.out.println(jObject);
This is what I am getting:
{"item_ref":"251","items":[{"item_ref":241,"price":100}]}
What I want is {"items":[{"item_ref":251,"price":100}]}
I also tried
JSONObject jObject = new JSONObject(jsonbody);
jObject.getJSONObject("items").remove("item_ref");
jObject.getJSONObject("items").put("item_ref","251");
System
But it says JSONObject["items"] is not a JSONObject.
All I need is to replace the 241 with 251. Is there an easier way to do this?
In general if we have a predefined JSON body file and if we want to replace some of the values in the body and use that in our POST calls within RestAssured, is there any easier way to do it?
The problem is - field item_ref and price are not in JSON Object as you think they are.
They are in JSON Array which contains JSON Objects. In order to modify that value, you have to get elements of the array and THEN execute very similar code you wrote.
Check this out:
JSONObject jObject = new JSONObject(jsonbody);
JSONArray array = jObject.getJSONArray("items");
JSONObject itemObject = (JSONObject) array.get(0); //here we get first JSON Object in the JSON Array
itemObject.remove("item_ref");
itemObject.put("item_ref", 251);
The output is:
{"items":[{"item_ref":251,"price":100}]}
Also, you can create a Hashmap:
HashMap<String,String> map = new HashMap<>();
map.put("key", "value");
RestAssured.baseURI = BASE_URL;
RequestSpecification request = RestAssured.given();
request.auth().preemptive().basic("Username", "Password").body(map).put("url");
System.out.println("The value of the field after change is: " + map.get("key"));

convert ElasticSearch SearchResponse object to JsonObject

I want to convert the elasticsearch search result to Json Object. I havent found any proper way to convert directly.
SearchResponse response = client.prepareSearch(index).setExplain(true).execute().actionGet();
response->JSON Object.
Is there any way to convert an ElasticSearch response to a Json Object?
In Java, you can directly convert the SearchResponse to JSONObject.
Below is the handy code.
SearchResponse SR = builder.setQuery(QB).addAggregation(AB).get();
JSONObject SRJSON = new JSONObject(SR.toString());
You need to use the SearchResponse.toXContent() method like this:
SearchResponse response = client.prepareSearch(index).setExplain(true).execute().actionGet();
XContentBuilder builder = XContentFactory.jsonBuilder();
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
JSONObject json = new JSONObject(builder.string());

JSON Parse using C#

Anybody has any idea how to parse below type of JSON object where same object named objects are present in master object. Below is an example
abc{
"abc":"123"
}
How to parse the child abc value?
You can use Json.NET it is vary easy to use. Json.NET handles JSON arrays natively, and will parse them into any type, string,int etc.
Are you currently using a JSON framework? Maybe check out Json.NET: http://www.newtonsoft.com/json.
Then you could do something like:
int onetwothree = jObject.SelectToken("abc.abc");
You can use http://www.newtonsoft.com/json to parse any json string on c#:
Your example :
var jsonString= "{\"abc\":{ \"abc\":\"123\" }}";
var jObj = JObject.Parse(jsonString);
var abcValue = jObj.SelectToken("abc.abc").Value<string>();

How to convert a javascript object to DyanamoDb JSON format?

JSON.stringify(obj) converts the object into normal JSON format.
How to convert a javascript object to DyanamoDb JSON format ?
Not sure what you mean by "DynamoDB JSON format".
JSON, is JSON, but here is an example of putting a JSON object into DynamoDB:
Document doc = Document.FromJson( itemJson );
table.PutItem( doc);
This library does exactly what I wanted to do:
"Translates sane javascript objects (and JSON) into DynamoDb format and vice versa."
https://www.npmjs.com/package/dynamodb-marshaler

Use a JSON with JSONOBJECT in Java

I want to use the following JSON in my code in Java:
[
{
"speed": 200
}
]
I tried to write this line :
JSONObject jsonBody = new JSONObject("[{\"speed\": 200}]");
But I always get an error. What is the exact format that I have to use here??
EDIT: So it would be more proper to use a JSONArray rather than use a JSONObject (as my last post said)
So the correct java code would be
String json = "[{\"speed\": 200}]";
System.out.println(json);
JSONArray jsonBody = new JSONArray(json);
System.out.println(jsonBody.getJSONObject(0).get("speed"));