set object value to another object in a JSON - json

I just wanted to know if I can do something like this in JSON:
{
"web" : {
"app_pub" : "localhost/public",
"app_lib" : "localhost/lib",
"app_assets" : "app_pub" + "/assets"
}
}

You can check your JSON, using validator, i.e.:
https://jsonformatter.curiousconcept.com/
Your code doesn't correspond RFC4627 and others.
You can't do operations inside JSON.
P.S.:
RFC4627 contains full JSON grammar.

Related

How to read values from nested JSON structure in java?

How to read values from nested JSON without using any library like GSON or org.JSON?
JSON is :
{data: { "EV_TOT_AMT" : "12" , "EV_CURR" : "INR", "T_BASKET" : [{"ORDER" : "abc", "BASE" : "xyx"},{"ORDER" : "def", "BASE" : "mno"}] } }
I want to read specific values as EV_TOT_AMT , EV_CURR , ORDER.
As far as I know, Java doesn't includes a JSON parser inside it's core classes... so if you don't want to use an external library, you'll need to build your own JSON parser.
Of course, you can just search the JSON string for your desired substrings and get the values moving into the string from the next ":" to the next "," (if the first character after the ":" is not an "["). But this isn't a good approach, unless your JSON input string is going to have always the same structure... well... actually that's not a good approach, period.

Is this valid JSON for parsing?

I've got a task to write a JSON parser in java with a little help.
I'm already able to parse this:
{
"ArrayWithOneString" : [ "ArrayContent" ],
"Array" : [
{
"ArrayinArray" : [
{
"NumberInArray" : 1337,
"StringInArray" : "String"
}
]
}
]
}
I've got only one last problem:
"string" : { // The bracket
"string" : "valueString"
},
My problem is that I expect a value and not another object for this opening bracket ({).
I wanted to ask if this is valid json before trying to parse it.
Yes it is valid. Well without your highlighting attempts, and assuming it is part of a parent object.
Just because you have a property called "string" doesn't mean it has to be a string value. I suggest perhaps whoever made it just isn't being very consistent, but it is still valid.
The question is, why are you expecting a value? Either the person who constructed the JSON has not done it to specification, or it is you that is not understanding the specification.
Also, you can easily validate JSON here.
Look here for specifications. Your example is valid according to this.
Yes it is valid JSON. You can now parse in your code.
You can check Valid JSON
See below screenshot.

Controlling JSON output format

A reverse to the question asked at How to format Json output ...
EclipseLink makes JSON output code from JAXB objects like this:
{
"test" : {
"count" : 10,
"text" : "Craig"
}
}
How do I make it put quotes around the number?
Thanks!

Extract array to JSON

I have a query that extracts a document with a username and password as a filter. My return value is an array (It will return the elements in phoneBook). How do I turn this into a JSON object? Just returning a JSON from a query would be ideal.
db.users.find({userName:"mark", passWord:"test1234"}, {phoneBook:1,_id:0})
{ "phoneBook" : [ { "firstName" : "Rupert", "lastName" : "Styx", "phoneNumber" : "9147388152", "email" : "ruperstyx#gmail.com" } ] }
I'm still playing around with this string. I can't extract the attributes inside of phoneBook and turn it into a JSON
If you use php then you can use jsonencode($your_array) function for converting an array into json object and if you working on other language then just search function for converting an array into json.
This is snipt from my code (my app in Node.js)
collection.find({
"word": req.query.word
}).toArray(function(err, results) {
var res = JSON.stringify(results);
});
Simply use JSON.stringify(results)
I hope it works for you
By using Robomongo tool you will be able to see query results in list, table and json view!
Using robomongo:
db.getCollection('ModelName')
.find({})
.limit(20)
.map(function(model){
return model.toSource()
})

Can you flatten a nested JSON structure into a single autobean?

Assuming the following JSON structure:
{
\"is_something\": false,
\"name\": \"Some Name\",
\"subtype\": {
\"total\": 0.0
}
}
Instead of creating two autobean interfaces (one for the whole structure and one for the subtype), I would like to have one which contains all the properties.
public interface ExampleAutoBean {
#PropertyName("is_something")
boolean isSomething();
String getName();
#PropertyName("subtype.total")
double getTotal();
}
So, the getTotal() method is expected to contain the total property of the nested subtype in the JSON structure. I can't find any documentation in the source code or online which states whether or not this is possible.
Thanks in advance!
Nope: AutoBeans are designed to be a mapping from the JSON structure to Java interfaces, plus or minus collections like List, Set, and Map and String encodings of a long or a Date. Additionally, it is legal to have json like the following:
{
"some.property.with.dots" : "abcd",
"name" : "wxyz"
}
If the . character could only be used for traversing into sub-objects, there would be no way to have a getter for the first property.