0

I´m trying to recreate this json:

{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "id": "90551bd1bcf6474dba09c96617a33638",
                    "password": "hoeIX1.i-.M]wiu9"
                }
            }
        },
        "scope": {
            "project": {
                "id": "2440e4fa1725452cb2e14506cb5d63ec"
            }
        }
    }
}

Right now I´m doing this:

JObject auth = new JObject(
    new JProperty("auth", new JObject(
        new JProperty("identity", new JObject(
            new JProperty("method",JArray.Parse("[\"password\"]")),
            new JProperty("password", new JObject(
                new JProperty("user", new JObject(
                    new JProperty("id", JValue.CreateString(identityWithProject.Username)),
                    new JProperty("password", JValue.CreateString(identityWithProject.ProjectName))))))),
        new JProperty("scope", new JObject(
            new JProperty("project", new JObject(
                new JProperty("id", JValue.CreateString(identityWithProject.ProjectName))))))))));

I get this error:

"Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray."

The line that is not working is:

new JProperty("method",JArray.Parse("[\"password\"]"))

If I remove the JArray part it works fine, how could I fix it so that it works with the array part? Most examples I have found are an array of JObjects which is not the case. Thanks!

2
  • 2
    This is a case where you would greatly benefit from using a class with the properties you need and serialize the class to return the json. Although you can do it the way you are, it is not readable and will be much harder to debug in the event of errors. That aside, you can use new JProperty("method", new JArray(new { "password" })). Commented Apr 29, 2016 at 15:13
  • I didn´t even thought about that, I will give it a try Commented Apr 29, 2016 at 15:48

1 Answer 1

3

It looks like there's a misplaced ), this works:

var auth = new JObject(
    new JProperty("auth", new JObject(
        new JProperty("identity", new JObject(
            new JProperty("methods", JArray.Parse("[\"password\"]")),
            new JProperty("password", new JObject(
                new JProperty("user", new JObject(
                    new JProperty("id", JValue.CreateString(identityWithProject.Username)),
                    new JProperty("password", JValue.CreateString(identityWithProject.ProjectName)))))))), // 8 x ')'
        new JProperty("scope", new JObject(
            new JProperty("project", new JObject(
                new JProperty("id", JValue.CreateString(identityWithProject.ProjectName))))))))); // 9 x ')'

I just moved one ) from the last line to the third from last line.

Also, the property "method" should be named "methods" if it's going to mach your sample. Furthermore, there's really no reason to use JValue.CreateString when creating your JProperties, and as @Steven Brickner points out, you can simplify JArray.Parse("[\"password\"]") to new JArray("password").

It might be more verbose, but for the sake of readability, here it is fully indented:

var auth = 
    new JObject(
        new JProperty("auth", 
            new JObject(
                new JProperty("identity", 
                    new JObject(
                        new JProperty("methods", new JArray("password")),
                        new JProperty("password", 
                            new JObject(
                                new JProperty("user", 
                                    new JObject(
                                        new JProperty("id", identityWithProject.Username),
                                        new JProperty("password", identityWithProject.ProjectName)
                                    )
                                )
                            )
                        )
                    )
                ),
                new JProperty("scope", 
                    new JObject(
                        new JProperty("project", 
                            new JObject(
                                new JProperty("id", identityWithProject.ProjectName)
                            )
                        )
                    )
                )
            )
        )
    );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I had try with new JArray before but it didn't work so I tried all the other possible ways without realizing the misplaced ' ('

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.