1

I am using jQuery Autocomplete, and when I select a record from the autocomplete results, it returns this as JSON:

{
    "label": "123 Fakeville St",
    "Address": "123 Fakeville St",
    "YOC": 1994,
    "value": "123 Fakeville St"
}

Is there a way I can reformat it so it looks like this?

{
    "house": {
        "properties": {
            "label": "123 Fakeville St",
            "Address": "123 Fakeville St",
            "YOC": 1994,
            "value": "123 Fakeville St"
        }
    }
}

1 Answer 1

1

Just put put the old JSON into a new JSON object under the property house:

var oldObj = JSON.parse(oldJson);
var newObj = {
    house: {
        properties: oldObj
    }
};
var newJson = JSON.stringify(newObj);

Or, in a single line, the value should be:

JSON.stringify({house:{properties:JSON.parse(oldJson)}})

var oldJson = '{"label":"123 Fakeville St","Address":"123 Fakeville St","YOC":1994,"value":"123 Fakeville St"}';

console.log(JSON.stringify({house:{properties:JSON.parse(oldJson)}}));

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! I also need the label, Address, YOC as part of "properties". Does this mean I have to do what you have suggested twice? Once for "house", and then for "properties"? Nest it twice...?
@mapr Oops, I misread the JSON you wanted. No, you don't have to do it twice. Give me a sec, I'll update my post...
Much simpler than what I was suggesting. Yes this works!
@mapr I'm glad I helped! (feel free to accept to show the community it helped you =P)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.